0

I'm experiencing some odd behavior when I bind() a callback to <Configure> on a Label I'm using to contain an image I want to resize as its containing Toplevel is resized.

According to the documentation, the <Configure> callback is supposed to get called when the user resizes the window (i.e. the Toplevel). What I'm finding is that the callback is getting called with no user interaction. In fact, it seems to be getting called when I update the image from the handler in response to the resize, which leads to an annoying loop which ends when the user moves (!) the window. Every call has event.width and/or event.height slightly larger or smaller, depending on how I've resized the image. It's almost as though the call I make to set the new image (Label.configure(image={newImage})) was triggering the callback, but this makes no sense.

I've tried tracing this with pdb, but all I find is a call coming from Tkinter.Tk.mainloop() with no indication of what led to it. I've also tried to duplicate this with a small demo program (which I could include here), but cannot, so I'm hoping someone might at least be able to answer the question:

Apart from user interaction (or any explicit call in my own code), what could cause a <Configure> callback to be called?

Thanks.

bobl
  • 331
  • 1
  • 2
  • 3

1 Answers1

1

The ` event is fired whenever the size of the widget changes. It isn't restricted to interactive resizing. If your binding changes the size of the widget, the event will be generated again.

According to the official tcl/tk documentation:

A Configure event is sent to a window whenever its size, position, or border width changes, and sometimes when it has changed position in the stacking order.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Thanks. That makes sense, but leads to the following conjecture: If I'm dynamically resizing an image to fit in the resized widget, if I make the image *exactly* the size specified in the configure event, it won't recursively generate another configure event, yes? – bobl Jun 23 '13 at 02:32
  • @user2507282: if your changes affect the size of the widget, you will get a `` event. If you make an image exactly the size of the widget, and there are any borders or highlight areas, or you use padding around the widget, then you will cause the size of the widget to change. – Bryan Oakley Jun 23 '13 at 03:41
  • Follow-up: padx and pady are both zero and I'm forcing the Label image to have the size exactly specified by event.width and event.height, yet Label.configure() *still* generates recursive '' events, this time with event.width and event.height exactly two pixels larger than the parent event. I suppose I could fake it out by making the image two pixels smaller in both dimensions, but this seems extreme, especially when I'd have to forgo maintaining aspect ratio in order for this to work. Incidentally, I'm using Grid, so border isn't an issue (or is it?). – bobl Jun 25 '13 at 23:01
  • labels also have a `borderwidth` and `highlightthickness` attribute which affects the overall size. If you're setting a label to an absolute size, you'll need to set those to zero. – Bryan Oakley Jun 26 '13 at 00:06
  • Eurekes! ("You have found it!") That fixes things nicely. Thanks! – bobl Jun 27 '13 at 23:25