Reputation: 5323
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.
How can one detect window minimize/maximize events? The event object returned by binding to a window's <Configure>
event does contain any information about these events. I've searched for protocols (like WM_DELETE_WINDOW
) that might expose these events without success.
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?
How can one determine a window's visibility, eg. whether a window is invisible or not as set by .withdraw()
?
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 <Configure>
event does not cancel window move or resize events.
Here's sample code for experimenting with Tkinter window events.
from __future__ import print_function
try:
import Tkinter as tk
except ImportError:
import tkinter as tk
def onFormEvent(event):
for key in dir(event):
if not key.startswith('_'):
print('%s=%s' % (key, getattr(event, key)))
print()
root = tk.Tk()
root.geometry('150x50')
lblText = tk.Label(root, text='Form event tester')
lblText.pack()
root.bind('<Configure>', onFormEvent)
root.mainloop()
Updat:e Here's what I learned about the following events:
event.type == 22
(one or more of following changed: width, height, x, y)event.type == 18
(minimized) event.widget.winfo_viewable() = 0
(invisible)event.type == 19
(restore after minimized)event.type == 2
(maximize)event.type == 22
(restore after maximized due to change in width and height)Upvotes: 4
Views: 8716
Reputation: 3764
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)
.
Upvotes: 3