Placing controls with grid() and pack()

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

Placing controls with grid() and pack()

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line places a Label in the first row, second column of a grid layout?
A lbl.grid(row=0, column=1)
B lbl.grid(row=1, column=0)
2. Which statement about pack() is correct?
A pack() needs a row and column number for every control
B pack() stacks controls in the order they are packed
3. You want a Label and an Entry side by side on one row. Which is correct?
A lbl.grid(row=0, column=0) and txt.grid(row=0, column=1)
B lbl.grid(row=0, column=0) and txt.grid(row=1, column=0)
4. Which line adds 10 pixels of space to the left and right of a control?
A btn.grid(row=2, column=0, pady=10)
B btn.grid(row=2, column=0, padx=10)
5. Which line makes a Button stretch across two columns?
A btn.grid(row=3, column=0, columnspan=2)
B btn.grid(row=3, column=0, colspan=2)

Match the Pairs

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

Left columnRight column
1label.grid(row=0, column=1) AThe argument name is wrong. grid() uses column, not col. It should be submit_button.grid(row=1, column=0).
2entry.pack() Bgrid() and pack() are mixed in the same window. This causes tkinter to fail or freeze. Use grid() for every control, e.g. name_entry.grid(row=0, column=1).
3Add 10 pixels of space on the left and right of a control in a grid layout CStacks the entry control in the window below the controls that were packed before it.
4Make a heading label stretch across two columns of the grid Dtitle.grid(row=0, column=0, columnspan=2)
5name_label.grid(row=0, column=0) name_entry.pack() EPlaces the label in the first row (row 0) and second column (column 1) of the window's table-like layout.
6submit_button.grid(row=1, col=0) Fbutton.grid(row=2, column=0, padx=10)

Definitions

control.grid(row=0, column=1)

Fill in the Blanks

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

1.
lblName.grid(___=0, column=0)
2.
entName.grid(row=0, ______=1)
3.
btnSave.grid(row=2, column=0, __________=2)
4.
lblName.grid(row=1, column=0, ____=8)
5.
btnOk.____(pady=5)

Spot the Error

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

Question 1
A import tkinter as tk
B win = tk.Tk()
C lb = tk.Label(win, text='Hi')
D lb.grid(row=0, column=1
E win.mainloop()
Question 2
A a = tk.Label(win, text='A')
B a.grid(row=0, column=0)
C b = tk.Label(win, text='B')
D b.pack()
Question 3
A b = tk.Button(win, text='Go')
B b.pack(padding=5)
C c = tk.Button(win, text='End')
D c.pack(padx=5, pady=5)
Question 4
A t = tk.Label(win, text='Menu')
B e = tk.Entry(win)
C e.grid(row=1, column=0)
D t.grid(row=0, colspan=2)
E win.mainloop()
Question 5
A lb = tk.Label(win, text='Age')
B lb.grid(0, 0)
C en = tk.Entry(win)
D en.grid(row=0, column=1)

Answer Key

Placing controls with grid() and pack()
Section A: Quick Questions
Q1
A — lbl.grid(row=0, column=1)
Q2
B — pack() stacks controls in the order they are packed
Q3
A — lbl.grid(row=0, column=0) and txt.grid(row=0, column=1)
Q4
B — btn.grid(row=2, column=0, padx=10)
Q5
A — btn.grid(row=3, column=0, columnspan=2)
Section B: Match the Pairs
Q1
label.grid(row=0, column=1) → Places the label in the first row (row 0) and second column (column 1) of the window's table-like layout.
Q2
entry.pack() → Stacks the entry control in the window below the controls that were packed before it.
Q3
Add 10 pixels of space on the left and right of a control in a grid layout → button.grid(row=2, column=0, padx=10)
Q4
Make a heading label stretch across two columns of the grid → title.grid(row=0, column=0, columnspan=2)
Q5
name_label.grid(row=0, column=0) name_entry.pack() → grid() and pack() are mixed in the same window. This causes tkinter to fail or freeze. Use grid() for every control, e.g. name_entry.grid(row=0, column=1).
Q6
submit_button.grid(row=1, col=0) → The argument name is wrong. grid() uses column, not col. It should be submit_button.grid(row=1, column=0).
Section C: Definitions
Q1
control.grid(row=0, column=1) — Places a control in a table-like layout at the given row and column. pack() instead stacks controls in order. Never mix grid and pack in one window.
Section D: Fill in the Blanks
Q1
lblName.grid(row=0, column=0)
Q2
entName.grid(row=0, column=1)
Q3
btnSave.grid(row=2, column=0, columnspan=2)
Q4
lblName.grid(row=1, column=0, padx=8)
Q5
btnOk.pack(pady=5)
Section E: Spot the Error
Q1
D — The grid() call is missing its closing bracket. It should be lb.grid(row=0, column=1) so the label is placed in row 0, column 1.
Q2
B — grid() and pack() must not be mixed in one window. The other control uses pack(), so this line should be a.pack().
Q3
B — There is no padding option for pack(). Spacing around a control uses padx and pady, so it should be b.pack(padx=5, pady=5).
Q4
D — colspan is the HTML name. In tkinter the option is columnspan, so it should be t.grid(row=0, columnspan=2).
Q5
B — grid() needs the keyword arguments row= and column=. Plain numbers do not work, so it should be lb.grid(row=0, column=0).