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.
17.4.1. Frame
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.
17.4.2. Toplevel
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.
deiconify | T.deiconify( )
Makes T display normally, even if previously T was iconic or invisible.
| geometry | T.geometry([geometry_string])
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.
| iconify | T.iconify( )
Makes T display as an icon (in Windows, as a button in the taskbar).
| maxsize | T.maxsize([width,height])
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.
| minsize | T.minsize([width,height])
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.
| overrideredirect | T.overrideredirect([avoid_decoration])
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.
| protocol | T.protocol(protocol_name,callable)
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.
| resizable | T.resizable([width,height])
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
| state | T.state( )
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).
| title | T.title([title_string])
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.
| withdraw | T.withdraw( )
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)
entry.focus_set( )
entry.pack( )
var.set(root.title( ))
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( )
Tkinter.mainloop( )
![](images/pixel.jpg) |