views:

48

answers:

1

I've been searching for information on the following Tkinter window features without success. Platform is Windows, Python 2.7. At the end of this post is code that can be used to explore Tkinter window events.

  1. How can one detect window minimize/maximize events? The event object returned by binding to a window's event does contain any information about these events. I've searched for protocols (like WM_DELETE_WINDOW) that might expose these events without success.

  2. How can one determine window frame border sizes (not Tkinter frames, the frame the OS places around the container where Tkinter places its widgets)? Is there a non-platform specific way to discover these windows properties or do I need to use platform specific solutions like the win32 api under Windows?

  3. How can one determine a window's visibility, eg. whether a window is invisible or not as set by .withdraw()?

  4. Is it possible to cancel a window event, eg. if one wanted to constrain a window to a certain location on a user's desktop? Returning 'break' from a window's event does not cancel window move or resize events.

Here's sample code for experimenting with Tkinter window events.

def onFormEvent( event ):
    for key in dir( event ):
        if not key.startswith( '_' ):
            print '%s=%s' % ( key, getattr( event, key ) )
    print

import Tkinter as tkinter
root = tkinter.Tk()
lblText = tkinter.Label( root, text='Form event tester' )
lblText.pack()
root.bind( '<Configure>', onFormEvent )
root.mainloop()

Update: Here's what I learned about the following events:

  1. event.type == 22 (one or more of following changed: width, height, x, y)

  2. event.type == 18 (minimized) event.widget.winfo_viewable() = 0 (invisible)

  3. event.type == 19 (restore after minimized)

  4. event.type == 2 (maximize)

  5. event.type == 22 (restore after maximized due to change in width and height)

+1  A: 

Determining window visibility is done with a .winfo_viewable() call. Returns 1 if visible, 0 if not.

If you want to prevent the window from resizing, set up your window the way you want, then use

self.root.minsize(self.root.winfo_reqwidth(), self.root.winfo_reqheight())
self.root.maxsize(self.root.winfo_reqwidth(), self.root.winfo_reqheight())

at the end of your __init__ call.

To completely disable the window from being moved, then you probably just want to remove the title bar and frame with self.root.overrideredirect(True).

Brandon Corfman
@Brandon Corfman: when a window is minimized or maximized you'll get a `<Configure>` event (though you get that same event for other things as well...). No need to use a timer.
Bryan Oakley
Well, that's good to know. I fixed my answer.
Brandon Corfman
Thank you Brandon and Bryan. I updated my original post with my findings.
Malcolm
Any ideas on how to cancel a window event (#4 above)? Oddly enough, returning 'break' from a window's <Configure> event does not cancel the event.
Malcolm
Brandon: Thank you very much for the update to your original answer.
Malcolm