Scientific Python 2025-03-04
Scientific Python 2025-03-04
Tk-inter
4-3-2025
● expand=YES option
○ Asks the packer to expand the allocated space for the
widget in general into any unclaimed space in the widget’s
parent.
● fill option
○ Can be used to stretch the widget to occupy all of its
allocated space.
● Label(text='hi').pack() # OK
● Label(text='hi').pack().mainloop() # wrong!
1. The packer starts out with an available space cavity that includes
the entire parent container (e.g., the whole Frame or top-level
window).
2. As each widget is packed on a side, that widget is given the entire
requested side in the remaining space cavity, and the space
cavity is shrunk.
3. Later pack requests are given an entire side of what is left, after
earlier pack requests have shrunk the cavity.
4. After widgets are given cavity space, expand divides any space
left, and fill and anchor stretch and position widgets within their
assigned space.
Tkinter: Packing Order
Label(win, text='Hello container world!').pack(side=TOP)
Button(win, text='Hello', command=greeting).pack(side=LEFT)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT)
The expand option tells the manager to assign additional space to the widget
box. If the parent widget is made larger than necessary to hold all packed
widgets, any exceeding space will be distributed among all widgets that have
the expand option set to a non-zero value.
Tkinter: Fill and expand
win = Frame()
win.pack()
Button(win, text='Hello', command=greeting).pack(side=LEFT,fill=Y)
Label(win, text='Hello container world').pack(side=TOP)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT, expand=YES, fill=X)
win = Frame()
win.pack(side=TOP, expand=YES, fill=BOTH)
Button(win, text='Hello', command=greeting).pack(side=LEFT, fill=Y)
Label(win, text='Hello container world').pack(side=TOP, expand=YES,fill=BOTH)
Button(win, text='Quit', command=win.quit).pack(side=RIGHT, expand=YES, fill=X)
Tkinter:
Using Anchor to Position
● The anchor option accepts tkinter constants identifying all eight
points of the compass (N, NE, NW, S, etc.) and CENTER as its
value
● The Hello button is anchored to the north, side:left of its space
allocation. Because this button was packed first, it got the entire
left side of the parent frame. This is more space than is needed to
show the button, so it shows up in the middle of that side by
default
Tkinter: Redefine a Callback
● The HelloButton class inherits everything from the tkinter Button
class, but adds a callback method and constructor logic to set the
command option to self.callback, a bound method of the instance.
● Instead of exiting, this MyButton button, when pressed, prints to
stdout and stays up
Tkinter widget classes
Label A simple message area
Button A simple labeled push-button widget
Frame A container for attaching and arranging other widget objects
Toplevel, Tk A new window managed by the window manager
Message A multiline label
Entry A simple single-line text-entry field
Checkbutton A two-state button widget, typically used for multiple-choice selections
Radiobutton A two-state button widget, typically used for single-choice selections
Scale A slider widget with scalable positions
PhotoImage An image object used for displaying full-color images on other widgets
BitmapImage An image object used for displaying bitmap images on other widgets
Menu A set of options associated with a Menubutton or top-level window
Menubutton A button that opens a Menu of selectable options and submenus
Scrollbar A control for scrolling other widgets (e.g., listbox, canvas, text)
Listbox A list of selection names
Text A multiline text browse/edit widget, with support for fonts, and so on
Canvas A graphic drawing area, which supports lines, circles, photos, text, and so on
Tkinter: Events
Some frequently used events
● Synonyms can be used for some common event names; for instance,
<ButtonPress-1>, <Button-1>, and <1> mean a left mouse button press, and
<KeyPress-a> and <Key-a> mean the “a” key. All forms are case sensitive:
use <Key-Escape>, not <KEY-ESCAPE>.
● Virtual event identifiers can be defined within double bracket pairs (e.g.,
<<PasteText>>) to refer to a selection of one or more event sequences.