|
1 |
| -# 17.6 - Control Layout With Geometry Managers |
2 |
| -# Review Exercise #2 |
3 |
| - |
4 |
| -# NOTE: The first exercise in this section is instructional and does |
5 |
| -# not require a solution to be shown here. For this reason, only the |
6 |
| -# solution to the second exercise is presented. |
7 |
| - |
8 | 1 | import tkinter as tk
|
9 | 2 |
|
10 |
| - |
11 |
| -# Create a new window with the title "Address Entry Form" |
| 3 | +# Creating a window with a title |
12 | 4 | window = tk.Tk()
|
13 | 5 | window.title("Address Entry Form")
|
14 | 6 |
|
15 |
| -# Create a new frame `frm_form` to contain the Label |
16 |
| -# and Entry widgets for entering address information. |
17 |
| -frm_form = tk.Frame(relief=tk.SUNKEN, borderwidth=3) |
18 |
| -# Pack the frame into the window |
19 |
| -frm_form.pack() |
20 |
| - |
21 |
| -# Create the Label and Entry widgets for "First Name" |
22 |
| -lbl_first_name = tk.Label(master=frm_form, text="First Name:") |
23 |
| -ent_first_name = tk.Entry(master=frm_form, width=50) |
24 |
| -# Use the grid geometry manager to place the Label and |
25 |
| -# Entry widgets in the first and second columns of the |
26 |
| -# first row of the grid |
27 |
| -lbl_first_name.grid(row=0, column=0, sticky="e") |
28 |
| -ent_first_name.grid(row=0, column=1) |
29 |
| - |
30 |
| -# Create the Label and Entry widgets for "Last Name" |
31 |
| -lbl_last_name = tk.Label(master=frm_form, text="Last Name:") |
32 |
| -ent_last_name = tk.Entry(master=frm_form, width=50) |
33 |
| -# Place the widgets in the second row of the grid |
34 |
| -lbl_last_name.grid(row=1, column=0, sticky="e") |
35 |
| -ent_last_name.grid(row=1, column=1) |
36 |
| - |
37 |
| -# Create the Label and Entry widgets for "Address Line 1" |
38 |
| -lbl_address1 = tk.Label(master=frm_form, text="Address Line 1:") |
39 |
| -ent_address1 = tk.Entry(master=frm_form, width=50) |
40 |
| -# Place the widgets in the third row of the grid |
41 |
| -lbl_address1.grid(row=2, column=0, sticky="e") |
42 |
| -ent_address1.grid(row=2, column=1) |
43 |
| - |
44 |
| -# Create the Label and Entry widgets for "Address Line 2" |
45 |
| -lbl_address2 = tk.Label(master=frm_form, text="Address Line 2:") |
46 |
| -ent_address2 = tk.Entry(master=frm_form, width=5) |
47 |
| -# Place the widgets in the fourth row of the grid |
48 |
| -lbl_address2.grid(row=3, column=0, sticky=tk.E) |
49 |
| -ent_address2.grid(row=3, column=1) |
50 |
| - |
51 |
| -# Create the Label and Entry widgets for "City" |
52 |
| -lbl_city = tk.Label(master=frm_form, text="City:") |
53 |
| -ent_city = tk.Entry(master=frm_form, width=50) |
54 |
| -# Place the widgets in the fifth row of the grid |
55 |
| -lbl_city.grid(row=4, column=0, sticky=tk.E) |
56 |
| -ent_city.grid(row=4, column=1) |
57 |
| - |
58 |
| -# Create the Label and Entry widgets for "State/Province" |
59 |
| -lbl_state = tk.Label(master=frm_form, text="State/Province:") |
60 |
| -ent_state = tk.Entry(master=frm_form, width=50) |
61 |
| -# Place the widgets in the sixth row of the grid |
62 |
| -lbl_state.grid(row=5, column=0, sticky=tk.E) |
63 |
| -ent_state.grid(row=5, column=1) |
64 |
| - |
65 |
| -# Create the Label and Entry widgets for "Postal Code" |
66 |
| -lbl_postal_code = tk.Label(master=frm_form, text="Postal Code:") |
67 |
| -ent_postal_code = tk.Entry(master=frm_form, width=50) |
68 |
| -# Place the widgets in the seventh row of the grid |
69 |
| -lbl_postal_code.grid(row=6, column=0, sticky=tk.E) |
70 |
| -ent_postal_code.grid(row=6, column=1) |
71 |
| - |
72 |
| -# Create the Label and Entry widgets for "Country" |
73 |
| -lbl_country = tk.Label(master=frm_form, text="Country:") |
74 |
| -ent_country = tk.Entry(master=frm_form, width=50) |
75 |
| -# Place the widgets in the eight row of the grid |
76 |
| -lbl_country.grid(row=7, column=0, sticky=tk.E) |
77 |
| -ent_country.grid(row=7, column=1) |
78 |
| - |
79 |
| -# Create a new frame `frm_buttons` to contain the |
80 |
| -# Submit and Clear buttons. This frame fills the |
81 |
| -# whole window in the horizontal direction and has |
82 |
| -# 5 pixels of horizontal and vertical padding. |
83 |
| -frm_buttons = tk.Frame() |
84 |
| -frm_buttons.pack(fill=tk.X, ipadx=5, ipady=5) |
85 |
| - |
86 |
| -# Create the "Submit" button and pack it to the |
87 |
| -# right side of `frm_buttons` |
88 |
| -btn_submit = tk.Button(master=frm_buttons, text="Submit") |
89 |
| -btn_submit.pack(side=tk.RIGHT, padx=10, ipadx=10) |
90 |
| - |
91 |
| -# Create the "Clear" button and pack it to the |
92 |
| -# right side of `frm_buttons` |
93 |
| -btn_clear = tk.Button(master=frm_buttons, text="Clear") |
94 |
| -btn_clear.pack(side=tk.RIGHT, ipadx=10) |
95 |
| - |
| 7 | +# List of fields we want to have in our form |
| 8 | +fields = ["First Name:", |
| 9 | + "Last Name:", |
| 10 | + "Address Line1:", |
| 11 | + "Address Line2:", |
| 12 | + "City:", |
| 13 | + "State/Province:", |
| 14 | + "Postal Code:", |
| 15 | + "Country:"] |
| 16 | + |
| 17 | +# The top frame contains the fields and their entry widgets |
| 18 | +top_frame = tk.Frame(relief=tk.SUNKEN, master = window) |
| 19 | +top_frame.pack() |
| 20 | + |
| 21 | +# We are going to use a 2 columns grid to place our widgets |
| 22 | +# We initialise the current row variable of our grid |
| 23 | +current_top_frame_row = 0 |
| 24 | + |
| 25 | +for field in fields: |
| 26 | + # For each field we create a label and entry widget |
| 27 | + label = tk.Label(text=field, master=top_frame) |
| 28 | + entry = tk.Entry(width=50, master=top_frame) |
| 29 | + # We place them in the current row of our grid |
| 30 | + label.grid(row=current_top_frame_row, column=0, sticky=tk.E) |
| 31 | + entry.grid(row=current_top_frame_row, column=1) |
| 32 | + # We increment the current line for the next field |
| 33 | + current_top_frame_row += 1 |
| 34 | + |
| 35 | +# Creating the bottom frame that contains |
| 36 | +# the Submit & Clear buttons |
| 37 | +bot_frame = tk.Frame(master=window) |
| 38 | +bot_frame.pack(fill=tk.X, ipadx=5, ipady=5) |
| 39 | +choices = ["Cancel", |
| 40 | + "Submit"] |
| 41 | + |
| 42 | +for choice in choices: |
| 43 | + # Create the button and pack it to the right side |
| 44 | + button = tk.Button(text=choice, relief=tk.RAISED, master=bot_frame) |
| 45 | + button.pack(side=tk.RIGHT, padx=5, ipadx=10) |
| 46 | + |
96 | 47 | # Start the application
|
97 | 48 | window.mainloop()
|
0 commit comments