Reputation: 305
When I try to detect that a window is resized with tkinter, it works most of the time. However, there is one exception; that is, when resizing it for the first time vertically and making the window larger, nothing is picked up. I see the initial size when the window appears, but nothing after that if I try to immediately make the window larger vertically. However, if I try to make it vertically "taller," after resizing it another way (ie making it smaller or resizing horizontally), then it works fine.
Here's the gist of the code I'm using:
from Tkinter import *
main = Tk()
def resize(event):
print("The new dimensions are:",event.width,"x",event.height)
window.focus_set()
canvas = Canvas(main, width=850, height=400)
window = Frame(main, width=800, height=10)
window.focus_set()
canvas.bind("<Configure>", resize)
canvas.pack()
canvas.update()
window.pack()
main.mainloop()
Am I doing something wrong? If it matters, I'm using Python 2.7.3 on 64 bit Ubuntu 12.10
EDIT: It seems as though the dimensions are either incorrect (horizontal enlargement) or not showing up at all (vertical enlargement) when the window size is above 852x402
. Is this a problem with my window manager (ie, Unity)?
Upvotes: 2
Views: 4323
Reputation: 20679
Change canvas = Canvas(main, width=850, height=400)
by canvas = Canvas(main, width=850, height=400, bg="red")
and see what happens.
Since the canvas is inside the root window, when you make the window vertically larger, you are not changing the canvas size or position. It maintains its maximum height of 400, and the handler is not called. However, in the case of the horizontal enlargement, its position is exactly the middle because of the call to pack()
: That's why it moves, and the bound function is called.
The rest of the results when you make the window smaller both horizontally and vertically are as expected.
Upvotes: 2
Reputation: 385900
It's because the canvas is not being resized. You're setting the binding on the canvas, but you haven't configured the canvas to grow and shrink along with the containing window.
Try setting the background of the canvas and frame widgets to something distinctive and you'll see what I mean.
To fix the problem try setting the expand
and fill
attributes when packing your widgets.
Upvotes: 2