0

I have a program that allows to pan and zoom the canvas. I have drawn a rectangle item (create_rectangle) and a text item (create_text) on the canvas. When I try to zoom out, the text is not in proportion with rectangle and covers the whole rectangle. Is there anyway to make text size in correspondence to zoom such that it will not cover the rectangle? Below is my code:

###################################################################
##### Imports #####
import random
import tkinter as tk
from tkinter import ttk

###################################################################
                         # FUNCTIONS #
###################################################################
def AutoScrollbar(lo, hi):
    if float(lo) <= 0.0 and float(hi) >= 1.0:
        ttk.Scrollbar.grid_remove()
    else:
        ttk.Scrollbar.grid()
    ttk.Scrollbar.set(lo, hi)

def wheel(mainCanvas, event, arg):
    ''' Mouse wheel zoom '''
    global x, y, delta
    scale = 1.0

    if event.delta == -120:
        scale *= delta
    if event.delta == 120:
        scale /= delta

    # Rescale all canvas objects
    x = mainCanvas.canvasx(event.x)
    y = mainCanvas.canvasy(event.y)
    mainCanvas.scale('all', x, y, scale, scale)
    mainCanvas.configure(scrollregion=mainCanvas.bbox('all'))

def move_from(mainCanvas, event, arg):
    ''' Remembering previous coordinates for scrolling with the mouse '''
    global x, y
    mainCanvas.scan_mark(event.x, event.y)

def move_to(mainCanvas, event, arg):
    ''' Draging (move) canvas to the new position '''
    global x, y
    mainCanvas.scan_dragto(event.x, event.y, gain=1)

###################################################################
                           # MAIN #
###################################################################
def main():
    root = tk.Tk()
    root.title('Scroll and zoom')
    root.geometry("600x600")
    mainFrame = ttk.Frame(root).grid()

    vertBar = ttk.Scrollbar(root, orient='vertical', command=AutoScrollbar)
    horiBar = ttk.Scrollbar(root, orient='horizontal', command=AutoScrollbar)
    vertBar.grid(row=0, column=1, sticky='ns')
    horiBar.grid(row=1, column=0, sticky='we')

    mainCanvas = tk.Canvas(mainFrame, highlightthickness=0, xscrollcommand=horiBar.set, yscrollcommand=vertBar.set)
    mainCanvas.grid(row=0, column=0, sticky='nswe')
    vertBar.configure(command=mainCanvas.yview)  # bind scrollbars to the canvas
    horiBar.configure(command=mainCanvas.xview)
    root.grid_rowconfigure(0, weight=1)
    root.grid_columnconfigure(0, weight=1)

    global delta
    delta = 0.75

    mainCanvas.bind('<ButtonPress-1>', lambda event, arg=mainCanvas: move_from(mainCanvas, event, arg))
    mainCanvas.bind('<B1-Motion>', lambda event, arg=mainCanvas: move_to(mainCanvas, event, arg))
    mainCanvas.bind('<MouseWheel>', lambda event, arg=mainCanvas: wheel(mainCanvas, event, arg))

    x0 =100
    y0 = 100
    x1 = 200
    y1 = 200
    rect = mainCanvas.create_rectangle(x0, y0, x1, y1, outline='black', fill='grey', activefill='grey')
    text = mainCanvas.create_text(100, 100, anchor='nw', text='Scroll to zoom', tags='text', width=0)
    mainCanvas.configure(scrollregion=mainCanvas.bbox('all'))


    root.mainloop()
###################################################################
                           # RUN #
###################################################################

def run():
    print('\nStart script')
    main()
    print('Finished script')

run()

Any help would be much appreciated. Thanks

  • 1
    @AhmedAEK: The answers to the [linked question](https://stackoverflow.com/questions/33795411/tkinter-scaling-items-on-a-canvas) provide some excellent suggestions on how to change the text size when zooming, however they do not handle the situation here because the font being used is the `TkDefaultFont` since the OP didn't specify one in the `create_text()` call. – martineau Mar 30 '22 at 13:38

0 Answers0