
|
Tkinter Lesson 5 Notes: A Wealth of Widgets
- Making "child" windows
- A root GUI window can launch "child" windows using Toplevel()
- root = Tk()
- win2 = Toplevel(root)
- Toplevel() requires the name of the associated root window.
- When the root window is destroyed, all associated Toplevel() windows will also be destroyed.
- Window Methods
win2.title("title text")
Adds title to top of window
win2.transient(root_window)
Makes window so it can close, but not minimize or resize.
win2.resizable(width=YES, height=NO)
Sets whether or not the window can be resized.
win2.maxsize(width=#, height=#)
Sets the maximum size of the window.
win2.minsize(width=#, height=#)
Sets the minimum size of the window.
- Toplevel() Example

from Tkinter import *
root = Tk()
root.title("Toplevels")
root.maxsize(width=300, height=200)
root.minsize(width=150, height=100)
root.resizable(width=YES, height=NO)
def child1():
c1 = Toplevel(root)
c1.title("Child One")
c1.transient(root)
Label(c1, text="This window is set as transient.").grid()
def child2():
c2 = Toplevel(root)
c2.title("Child Two")
Label(c2, text="This is a regular toplevel window.").grid()
Label(root, text="Toplevel Practice").grid(row=0, column=0, columnspan=2)
Button(root, text="Child 1", command = child1).grid(row=1, column=0)
Button(root, text="Child 2", command = child2).grid(row=1, column=1)
- Control Variables
- Control Variables are variables that can hold values for GUI widgets to access and update as needed
- There are two types of control variables:
IntVar() -- holds integers
StringVar() -- hold string values
- Set the value of a control variable using set()
var = IntVar()
var.set(1)
- Get the value out of a control variable using get()
number = var.get()
- To use control variables:
- Assign the control variable class to a name
num_holder = IntVar()
- Set widget to use that variable
rb1 = Radiobutton(root, text = "Five", variable = num_holder, value=5, command = num_holder.set(value))
- Radiobutton()
- A Radiobutton() is a widget that allows the user to choose one out of a list of options.
- The Radiobutton() widget consists of a select button and text. You do not have to create a separate Label() for the radiobutton.
- Radiobuttons are designed so that each button passes a unique "value" to a common "variable". Other portions of the program can then see what the value of this variable is and act accordingly.
- To link the radiobuttons together, they must each be set to the same variable, and the variable must be a special "control variable"
- Example:
rb1 = Radiobutton(root, text = "First Choice", variable = num, value = 1)
rb2 = Radiobutton(root, text="Second choice", variable = num, value=2)
- Example Radiobuttons()

from Tkinter import *
root = Tk()
lbl = Label(root, text="Pre-click", bg="#ffffcc")
lbl.grid(sticky="EW")
foo = IntVar()
def text_update():
number = foo.get()
lbl.configure(text = number)
rb1 = Radiobutton(root, text = "First Choice", variable=foo, value=1, command = text_update)
rb1.grid()
rb2 = Radiobutton(root, text = "Second Choice", variable=foo, value=2, command = text_update)
rb2.grid()
rb3 = Radiobutton(root, text = "Third Choice", variable=foo, value=3, command = text_update)
rb3.grid()
- Checkbutton()
- Checkboxes provide non-exclusive choices (the user can pick more than one).
- Checkboxes are usually linked to a control variable, and provide both an "onvalue" and an "offvalue"
- Checkbox Example

from Tkinter import *
root = Tk()
x1 = StringVar()
name1 = Checkbutton(root, text = "Pizza", variable = x1, onvalue="Pizza\n", offvalue="")
name1.grid()
x2 = StringVar()
name2 = Checkbutton(root, text="Salad", variable = x2, onvalue="Salad\n", offvalue="")
name2.grid()
x3 = StringVar()
name3 = Checkbutton(root, text="Soda", variable = x3, onvalue = "Soda\n", offvalue="")
name3.grid()
x4 = StringVar()
name4 = Checkbutton(root, text="Dessert", variable = x4, onvalue="Dessert\n", offvalue="")
name4.grid()
def ourmenu():
menu = x1.get() + x2.get() + x3.get() + x4.get()
lbl.configure(text = menu)
btn = Button(root, text="Place Order", command = ourmenu)
btn.grid()
lbl = Label(root, text = "", bg = "red", fg="white")
lbl.grid(sticky=EW)
- Listbox()
- Creates a box with choices for the user to select.
- Options include:
selectmode = SINGLE
Determines how items in list may be selected. Choices are SINGLE, MULTIPLE, BROWSE, or EXTENDED
xscrollcommand OR yscrollcommand
Links the list box to a scroll bar.
height = # , width = #
Sets the height or width of the listbox.
- Listbox() Methods
listbox.activate(index)
Selects the item in the listbox at the given index
-
listbox.curselection()
Returns the numerical indices for the currently selected choices
listbox.delete(index)
Deletes the choice at the given index
listbox.index(index)
Adjusts the listbox so that the choice at the given index is displayed in the top of the window
listbox.insert(index, choice)
Adds choices to the listbox. Index may be a numerical value or the keyword END
listbox.selection_clear(index)
Deselects the choice at the given index
listbox.selection_includes(index)
Returns TRUE if the choice at the given index is currently selected
listbox.selection(index)
Selects the choice at the given index.
- Listbox() Example

from Tkinter import *
root = Tk()
root.title("Listbox")
lbl = Label(root, text = "", bg="white", fg="midnightblue", font = ("arial", 16))
lbl.grid()
choices = ["Red", "Orange", "Yellow", "Green", "Blue", "Purple"]
list = Listbox(root, height=6, selectmode = MULTIPLE)
list.grid()
for item in choices:
list.insert(END, item)
def view_selected():
x = list.curselection()
lbl.configure(text = x)
btn = Button(root, text = "Click for values", command = view_selected)
btn.grid()
- Scrollbar()
- A Scrollbar() is a widget that allows the information in another widget to be scrolled.
- Scrollbars must be bound to scrollable widgets, such as Listbox(), Text(), and Canvas()
- Scrollbars may be horizontal (xview), vertical (yview) or both.
- To bind a scrollbar to a scrollable widget, use the command = "" option in the scrollbar, then set the command to widget.xview.
Example:
listbox = Listbox(root)
scroll = Scrollbar(root, command = listbox.xview)
listbox.configure(xscrollcommand = scroll.set)
- Scrollbar Options
-
troughcolor = "color_name"
Sets color of the slider trough
orient = VERTICAL
Sets orientation of the scrollbar. Options are VERTICAL and HORIZONTAL.
- Scrollbar() Example

from Tkinter import *
root = Tk()
root.title("Listbox")
lbl = Label(root, text = "", bg="white", fg="midnightblue", font = ("arial", 16))
lbl.grid(row = 0, column = 0, columnspan = 2)
choices = ["Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Orchid", "Beige"]
list = Listbox(root, height=4, selectmode = MULTIPLE)
for item in choices:
list.insert(END, item)
scroll = Scrollbar(root, command = list.yview)
list.configure(yscrollcommand = scroll.set)
list.grid(row = 1, column = 0, sticky = NS)
scroll.grid(row = 1, colum = 1, sticky = NS)
def view_selected():
x = list.curselection()
lbl.configure(text = x)
btn = Button(root, text = "Click for values", command = view_selected)
btn.grid(row = 2, column = 0, columnspan = 2)
- Scale()
- Scale() creates a sliding scale widget for selecting numbers
- Methods for scale:
scale.get()
Retrieves the current value of the scale
scale.set()
Sets the slider at a certain value on the scale
- Options for Scale()
- tickinterval = #
Determines which guide numbers are displayed on scale
from_ = #
Lowest number for scale
to = #
Highest number for scale
orient = VERTICAL
Whether scale should be HORIZONTAL or VERTICAL
resolution = #
Numerical distance between each value on the scale
showvalue = TRUE
Whether of not to display which number is currently selected
troughcolor = "colorname"
What color should the slider trough be
- Scale() Example

from Tkinter import *
root = Tk()
root.title("Scale")
lbl = Label(root, text = "", bg = "white", \
fg = "seagreen", font=("arial", 16, "bold") )
lbl.grid()
def getnum(event):
x = scale.get()
lbl.configure(text = x)
scale = Scale(root, orient=HORIZONTAL, length = 100, \
from_=0, to=100, tickinterval=50, \
command = getnum)
scale.set(0)
scale.grid()
- Message()
- The Message() widget is a quick way to display multi-line text.
- In Message, the text = "" portion can span more than one line.
-
Example:
msg = Message(root, text = "Here is line one"
"Here is line two"
"Here is line three"
"That's the end of my message text")
- Message Example:

from Tkinter import *
root = Tk()
def message():
rt2 = Toplevel(root)
msg = Message(rt2, text="Here is the first line of text. "
"Here is the next line of text. "
"Now we are on line three. "
"Oooh, look mom, line four! "
"Okay, that's enough. Goodbye.", bg="white", fg="red")
msg.grid()
rt2.transient(root)
Button(root, text = "Message", command = message).grid()
Restricted access |