Python Forum

Full Version: canvas size
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,
I have an app with a large canvas, showing images.
The canvas is configured:
tifCanvas = Canvas(root, width = 1200, height = 950, relief=RAISED,bg='lightgreen')
tifCanvas.pack(side=LEFT,fill=BOTH,padx=10,pady=10)
I can check this when the app starts by printing:
print('w:',tifCanvas.winfo_reqwidth(),'h:',tifCanvas.winfo_reqheight())
It says : 1204 x 954. OK, so far, so good.
But my monitor is much bigger (1920 x 1200)
When I click (top right) to maximize my app window to fit the monitor,
the canvas clearly gets bigger (fill BOTH), the image stays the same, and when i click a secret button
to print the canvas size at that moment, it slill says: 1204 x 954.
If i can get the correct canvas size at any moment, i could resize my image to be bigger.
But how to find it?
thx,
Paul
Maybe this will help
import tkinter as tk

root = tk.Tk()
canvas = tk.Canvas(root, bg='green', width=1200, height=950)
canvas.pack(fill='both', expand = True, padx=10, pady=10)
mytext = canvas.create_text(0, 0, fill='lime', tags=['event'])

def show(event):
    canvas.itemconfigure('event', text=f'Width -> {event.width} Height -> {event.height}')
    xpos = (event.width/2)
    ypos = (event.height/2)
    canvas.coords(mytext, xpos, ypos)
    if event.width > 1210:
        canvas.itemconfigure('event', font=('helvetica 60 bold'))
    else:
        canvas.itemconfigure('event', font=('helvetica 30 normal'))
canvas.bind('<Configure>', show)
root.mainloop()
Indeed, it does help to show the difference in size.
I found one way to solve the inconvenience by adding:
root.state('zoomed')
So my canvas now always starts with the max size.
thx,
Paul