17.4. Container Widgets
The Tkinter module supplies widgets whose purpose is to contain other widgets. A Frame instance does nothing else but act as a container.
A Toplevel instance (including Tkinter's root window, also known as the application's main window) is a top-level window, so your window manager interacts with it (typically by supplying suitable decoration and handling requests). To ensure that a widget parent, which must be a Frame or Toplevel instance, is the parent (a.k.a. the master) of another widget child, pass parent as the first parameter when you instantiate child.
Class Frame is a rectangular area of the screen contained in other frames or top-level windows. Frame's only purpose is to contain other widgets. Option borderwidth defaults to 0, so an instance of Frame normally displays no border. You can configure the option with borderwidth=1 if you want the frame border's outline to be visible.
Class Toplevel represents a rectangular area of the screen that is a top-level window and therefore receives decoration from whichever window manager handles your screen. Each instance of Toplevel interacts with the window manager and contains other widgets. Every Tkinter program has at least one top-level window, known as the root window. Instantiate Tkinter's root window with root=Tkinter.Tk( ); otherwise, Tkinter instantiates its root window implicitly as and when it is first needed. If you want more than one top-level window, instantiate the main one with root=Tkinter.Tk( ). Later, instantiate other top-level windows as needed, with calls such as another_toplevel=Tkinter.Toplevel( ).
An instance T of class Toplevel supplies many methods that enable interaction with the window manager. Many are platform-specific, relevant only with some window managers for the X Window System (used mostly on Unix-like systems). The cross-platform methods used most often are as follows.
Makes T display normally, even if previously T was iconic or invisible.
T.geometry( ), without arguments, returns a string that encodes T's size and position: widthxheight+x_offset+y_offset, with width, height, x_offset, and y_offset being decimal forms of the numbers of pixels. T.geometry(S), with one argument S (a string of the same form), sets T's size and position according to S.
Makes T display as an icon (in Windows, as a button in the taskbar).
T.maxsize( ), without arguments, returns a pair of integers whose two items are T's maximum width and height in pixels. T.maxsize(w,h), with two integer arguments w and h, sets T's maximum width and height in pixels to w and h, respectively.
T.minsize( ), without arguments, returns a pair of integers whose two items are T's minimum width and height in pixels. T.minsize(w,h), with two integer arguments w and h, sets T's maximum width and height in pixels to w and h, respectively.
T.overrideredirect( ), without arguments, returns False for a normal window, and TRue for a window that has asked the window manager to avoid decorating it. T.overrideredirect(x), with one argument x, asks the window manager to avoid decorating T if, and only if, x is true. A top-level window T without decoration has no title, and the user cannot act via the window manager to close, move, or resize T.
By calling protocol with a first argument of 'WM_DELETE_WINDOW' (the only valid value on most platforms), you install callable as the handler for attempts by the user to close T tHRough the window manager (e.g., by clicking on the X in the upper-right corner on Windows and KDE). Python calls callable without arguments when the user makes such an attempt. callable itself must call T.destroy( ) to close T; otherwise, T stays open. By default, if T.protocol has not been called, such attempts implicitly call T.destroy( ) and thus unconditionally close T.
T.resizable( ), without arguments, returns a pair of integers (each 0 or 1) that indicate if user action via the window manager can change T's width and height, respectively. T.resizable(w,h), with two integer arguments w and h (each 0 or 1), sets the user's ability to change T's width and height accordingly. With some releases of Tk, resizable, when called without arguments, returns a string such as '1 1' rather than a pair of integers such as (1,1). To remove this uncertainty, use:
resizable_wh = T.resizable( )
if len(resizable_wh) != 2: resizable_wh
= map(int, resizable_wh.split( ))
resizable_w, resizable_h = resizable_wh
Returns 'normal' if T is showing normally, 'withdrawn' if T is invisible, or 'icon' or 'iconic' (depending on the window manager) if T is showing as an icon (e.g., in Windows, only as a button in the taskbar).
T.title( ), without arguments, returns a string that is T's window title. T.title(title_string), with one argument title_string, sets T's window title to string title_string.
Makes T invisible.
The following example shows a root window with an Entry widget that lets the user edit the window's title, and buttons to perform various root window operations:
import Tkinter root = Tkinter.Tk( )
var = Tkinter.StringVar( )
entry = Tkinter.Entry(root, textvariable=var)
def changeTitle( ): root.title(var.get( ))
Tkinter.Button(root, text="Change Title", command=changeTitle).pack( )
Tkinter.Button(root, text="Iconify", command=root.iconify).pack( )
Tkinter.Button(root, text="Close", command=root.destroy).pack( )