Previous Section Next Section

16.4 Container Widgets

The Tkinter module supplies widgets whose purpose is to contain other widgets. A Frame instance does nothing more than 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 certain requests). To ensure that a widget parent, which must be a Frame or Toplevel instance, is the parent (also known as master) of another widget child, pass parent as the first parameter when you instantiate child.

16.4.1 Frame

Class Frame represents 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.

16.4.2 Toplevel

Class Toplevel represents a rectangular area of the screen that is a top-level window and therefore receives decoration from whatever window manager handles your screen. Each instance of Toplevel can interact with the window manager and can contain other widgets. Every program using Tkinter has at least one top-level window, known as the root window. You can instantiate Tkinter's root window explicitly using root=Tkinter.Tk( ); otherwise Tkinter instantiates its root window implicitly as and when first needed. If you want to have more than one top-level window, first instantiate the main one with root=Tkinter.Tk( ). Later in your program, you can instantiate other top-level windows as needed, with calls such as another_toplevel=Tkinter.Toplevel( ).

An instance T of class Toplevel supplies many methods enabling interaction with the window manager. Many are platform-specific, relevant only with some window managers for the X Windowing System (used mostly on Unix and 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 encoding T's size and position: widthxheight+x_offset+y_offset, with width, height, x_offset, and y_offset being the decimal forms of the corresponding 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.deiconify(  )

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 minimum 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, 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 without decoration has no title. The user cannot act via the window manager to close, move, or resize such an undecorated top-level window.

protocol

T.protocol(protocol_name,callable)

By calling protocol with a first argument of 'WM_DELETE_WINDOW' (the only meaningful protocol on most platforms), you install callable as the handler for attempts by the user to close T through the window manager (for example by clicking on the X in the upper right corner on Windows and KDE). Python then calls callable without arguments when the user makes such an attempt. callable itself must call T.destroy( ) in order 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) whose two items 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 according to the truth values of W and H. 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 displaying normally, 'withdrawn' if T is invisible, 'icon' or 'iconic' (depending on the window manager) if T is displaying 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(  )
    Previous Section Next Section