Choice controls: Checkbutton, Radiobutton and Combobox

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Choice controls: Checkbutton, Radiobutton and Combobox

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line creates a variable to store whether a Checkbutton is ticked?
A agree = tk.BooleanVar()
B agree = tk.Checkbutton()
2. A Radiobutton group lets the user pick one size. How do you make the buttons work as one group?
A Give every Radiobutton its own separate variable but the same value=
B Give every Radiobutton the same variable=size but a different value=
3. Which Combobox call correctly supplies the list of options?
A ttk.Combobox(root, options=["Red", "Blue"])
B ttk.Combobox(root, values=["Red", "Blue"])
4. A StringVar called colour is linked to a choice control. How do you read the user's choice?
A colour.get()
B colour.value
5. How do you link a Combobox to a StringVar called pick?
A ttk.Combobox(root, textvariable=pick)
B ttk.Combobox(root, variable=pick)

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1Radiobutton(root, text="Small", variable=size, value="S") Asize = StringVar() Radiobutton(root, text="S", variable=size, value="S") Radiobutton(root, text="L", variable=size, value="L")
2ttk.Combobox(root, textvariable=colour, values=["Red", "Blue"]) BCreates one radio option that sets the shared variable size to "S" when the user selects it
3A Checkbutton stores True or False in a BooleanVar, so you read the tick state with .get() CNo value= is given, so the buttons have no distinct value to store and cannot be told apart. Add value="S" and value="L".
4A group of Radiobuttons shares ONE variable so that only one option can be selected at a time Dagree = BooleanVar() Checkbutton(root, text="I agree", variable=agree) print(agree.get())
5Radiobutton(root, text="Small", variable=size) Radiobutton(root, text="Large", variable=size) EThe variable is printed instead of reading its contents. Use choice.get() to get the user's pick.
6choice = StringVar() print(choice) FCreates a drop-down list whose chosen item is stored in the variable colour

Definitions

StringVar()

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
ok = tk.__________() tk.Checkbutton(win, text="Yes", variable=ok)
2.
tk.Radiobutton(win, variable=size, _____="S")
3.
size = tk.StringVar(value="S") tk.Radiobutton(win, variable=____, value="M")
4.
box = ttk.Combobox(win, ______=["S", "M"])
5.
size = tk.StringVar(value="S") print("Chosen:", size.___())

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A choice = tk.StringVar
B r1 = tk.Radiobutton(win)
C r1.config(text="Yes", value="y")
D r1.config(variable=choice)
Question 2
A agree = tk.BooleanVar()
B cb = tk.Checkbutton(win)
C cb.config(variable="agree")
D print(agree.get())
Question 3
A size = tk.StringVar()
B cb = ttk.Combobox(win)
C cb.config(textvariable=size)
D cb.values = ["S", "M"]
Question 4
A pick = tk.StringVar()
B pick.set("Medium")
C print(pick.value())
Question 5
A pet = tk.StringVar()
B r1 = tk.Radiobutton(win)
C r1.config(textvariable=pet)
D r2 = tk.Radiobutton(win)
E r2.config(variable=pet)

Answer Key

Choice controls: Checkbutton, Radiobutton and Combobox
Section A: Quick Questions
Q1
A — agree = tk.BooleanVar()
Q2
B — Give every Radiobutton the same variable=size but a different value=
Q3
B — ttk.Combobox(root, values=["Red", "Blue"])
Q4
A — colour.get()
Q5
A — ttk.Combobox(root, textvariable=pick)
Section B: Match the Pairs
Q1
Radiobutton(root, text="Small", variable=size, value="S") → Creates one radio option that sets the shared variable size to "S" when the user selects it
Q2
ttk.Combobox(root, textvariable=colour, values=["Red", "Blue"]) → Creates a drop-down list whose chosen item is stored in the variable colour
Q3
A Checkbutton stores True or False in a BooleanVar, so you read the tick state with .get() → agree = BooleanVar() Checkbutton(root, text="I agree", variable=agree) print(agree.get())
Q4
A group of Radiobuttons shares ONE variable so that only one option can be selected at a time → size = StringVar() Radiobutton(root, text="S", variable=size, value="S") Radiobutton(root, text="L", variable=size, value="L")
Q5
Radiobutton(root, text="Small", variable=size) Radiobutton(root, text="Large", variable=size) → No value= is given, so the buttons have no distinct value to store and cannot be told apart. Add value="S" and value="L".
Q6
choice = StringVar() print(choice) → The variable is printed instead of reading its contents. Use choice.get() to get the user's pick.
Section C: Definitions
Q1
StringVar() — A tkinter variable that stores the user's pick from a choice control (Checkbutton, Radiobutton or Combobox). Link it with variable= and read the choice with .get().
Section D: Fill in the Blanks
Q1
ok = tk.BooleanVar() tk.Checkbutton(win, text="Yes", variable=ok)
Q2
tk.Radiobutton(win, variable=size, value="S")
Q3
size = tk.StringVar(value="S") tk.Radiobutton(win, variable=size, value="M")
Q4
box = ttk.Combobox(win, values=["S", "M"])
Q5
size = tk.StringVar(value="S") print("Chosen:", size.get())
Section E: Spot the Error
Q1
A — StringVar is a class, so it must be called with brackets to create the variable object. It should be choice = tk.StringVar().
Q2
C — variable= needs the tkinter variable object, not a string with its name. Remove the quotes: cb.config(variable=agree).
Q3
D — A Combobox list of options is set with the "values" option, not by creating a Python attribute. It should be cb["values"] = ["S", "M"].
Q4
C — A tkinter variable has no .value() method. The user's choice is read with .get(), so it should be print(pick.get()).
Q5
C — textvariable sets the text shown on a control. A Radiobutton stores the user's choice using variable=, so it should be r1.config(variable=pet), the same as r2.