Creating and Destroying Windows
Toplevel windows are created using the Toplevel function:
t = Toplevel(parent)
Unlike regular widgets, you don't have to "grid" a toplevel for it to appear onscreen. Once you've created a new toplevel,you can then create other widgets which are children of that toplevel, and grid them inside the toplevel. In other words,the new toplevel behaves exactly like the automatically created root window.
To destroy a window, you can use the destroy method on a widget:
window.destroy()
Note that you can use
destroy on any widget, not just a toplevel window.
Changing Window Behavior and Styles
Window Title
oldtitle = window.title()
window.title('New title')
Size and Location
A full geometry specification looks like this:
widthxheight±x±y
Width and height (normally in pixels) are pretty self-explanatory. The "x" (horizontal position) is specified with a leading plus or minus, so
"+25" means the left edge of the window should be 25 pixels from the left edge of the screen, while
"-50" means the right edge of the window should be 50 pixels from the right edge of the screen. Similarly, a "y" (vertical) position of "+10" means the top edge of the window should be ten pixels below the top of the screen, while "-100" means the bottom edge of the window should be 100 pixels above the bottom of the screen.
window.geometry('300x200-5+40')
Stacking Order
Stacking order refers to the order that windows are "placed" on the screen, from bottom to top.
You can obtain the current stacking order, a list from lowest to highest, via:
root.tk.eval('wm stackorder '+str(window))
You can also just check if one window is above or below another:
if (root.tk.eval('wm stackorder '+str(window)+' isabove '+str(otherwindow))=='1') ...
if (root.tk.eval('wm stackorder '+str(window)+' isbelow '+str(otherwindow))=='1') ...
You can also raise or lower windows, either to the very top (bottom) of the stacking order, or just above (below) a designated window:
window.lift()
window.lift(otherwin)
window.lower()
window.lower(otherwin)
So if you have several widgets gridded together but overlapping, you can raise and lower them relative to each other. For example:
from tkinter import *
from tkinter import ttk
root = Tk()
little = ttk.Label(root, text="Little")
bigger = ttk.Label(root, text='Much bigger label')
little.grid(column=0,row=0)
bigger.grid(column=0,row=0)
root.after(2000, lambda: little.lift())
root.mainloop()
Resizing Behavior
You can prevent it from being resized, in fact independently specifying whether the window's width (first parameter) can be changed, as well as its height (second parameter). So to disable all resizing:
window.resizable(FALSE,FALSE)
Remember that if you've added a
ttk::sizegrip widget to the window, that you should remove it if you're making the window non-resizable.
If resizing is enabled, you can specify a minimum and/or maximum size that you'd like the window's size to be constrained to (again, parameters are width and height):
window.minsize(200,100)
window.maxsize(500,500)
Iconifying and Withdrawing
The possible states for a window include "normal" and "iconic" (for an iconified window), as well as several others:"withdrawn", "icon" or "zoomed".
You can query or set the current window state, and there are also the methods "iconify" and"deiconify" which are shortcuts for setting the "iconic" or"normal" states respectively.
thestate = window.state()
window.state('normal')
window.iconify()
window.deiconify()
Standard Dialogs
Selecting Files and Directories
On Windows and Mac,these invoke the underlying operating system dialogs directly.
from tkinter import filedialog
filename = filedialog.askopenfilename()
filename = filedialog.asksaveasfilename()
dirname = filedialog.askdirectory()
All of these commands produce
modal dialogs. The commands return the full pathname of the file or directory the user has chosen, or return an empty string if the user cancels out of the dialog.
Selecting Colors
There is also a modal dialog to let the user select a color. It will return a color value, e.g. "#ff62b8".The dialog takes an optional"initialcolor" option to specify an existing color that the user is presumably replacing.
from tkinter import colorchooser
colorchooser.askcolor(initialcolor='#ff0000')
Alert and Confirmation Dialogs
Tk provides a versatile "message box" that encapsulates all these different types of dialogs.
from tkinter import messagebox
messagebox.showinfo(message='Have a good day')
messagebox.askyesno(
message='Are you sure you want to install SuperVirus?'
icon='question' title='Install')
Like the previous dialogs that we've seen, these are modal, and will return the result of the user's action to the caller. The exact return value will depend on the
"type" option passed to the command, as shown here:
Type option | Possible return values |
---|---|
ok (default) | "ok" |
okcancel | "ok" or "cancel" |
yesno | "yes" or "no" |
yesnocancel | "yes", "no" or "cancel" |
retrycancel | "retry" or "cancel" |
abortretryignore | "abort", "retry" or "ignore" |
type | As described above. |
message | The main message displayed inside the alert. |
detail | If needed, a secondary message, often displayed in a smaller font under the main message. |
title | Title for the dialog window. Not used on Mac OS X. |
icon | Icon to show: one of "info" (default), "error", "question" or"warning". |
default | Specify which button (e.g. "ok" or "cancel" for a "okcancel" type dialog) should be the default. |
parent | Specify a window of your application this dialog is being posted for; this may cause the dialog to appear on top, or on Mac OS X appear as a sheet for the window. |