From fbe86ddadcd39849e0f0e080b30eb810c5cd89ae Mon Sep 17 00:00:00 2001
From: Adam Makhlouf <tsakagur@gmail.com>
Date: Mon, 23 Dec 2019 11:18:32 +0100
Subject: [PATCH] Using loops

Just a proposition to use loops to create the form. I think it applies quite well in this case and makes the code easier to follow for everyone!
---
 ...6-control-layout-with-geometry-managers.py | 131 ++++++------------
 1 file changed, 41 insertions(+), 90 deletions(-)

diff --git a/ch17-graphical-user-interfaces/6-control-layout-with-geometry-managers.py b/ch17-graphical-user-interfaces/6-control-layout-with-geometry-managers.py
index 339c992..85fe94b 100644
--- a/ch17-graphical-user-interfaces/6-control-layout-with-geometry-managers.py
+++ b/ch17-graphical-user-interfaces/6-control-layout-with-geometry-managers.py
@@ -1,97 +1,48 @@
-# 17.6 - Control Layout With Geometry Managers
-# Review Exercise #2
-
-# NOTE: The first exercise in this section is instructional and does
-# not require a solution to be shown here. For this reason, only the
-# solution to the second exercise is presented.
-
 import tkinter as tk
 
-
-# Create a new window with the title "Address Entry Form"
+# Creating a window with a title
 window = tk.Tk()
 window.title("Address Entry Form")
 
-# Create a new frame `frm_form` to contain the Label
-# and Entry widgets for entering address information.
-frm_form = tk.Frame(relief=tk.SUNKEN, borderwidth=3)
-# Pack the frame into the window
-frm_form.pack()
-
-# Create the Label and Entry widgets for "First Name"
-lbl_first_name = tk.Label(master=frm_form, text="First Name:")
-ent_first_name = tk.Entry(master=frm_form, width=50)
-# Use the grid geometry manager to place the Label and
-# Entry widgets in the first and second columns of the
-# first row of the grid
-lbl_first_name.grid(row=0, column=0, sticky="e")
-ent_first_name.grid(row=0, column=1)
-
-# Create the Label and Entry widgets for "Last Name"
-lbl_last_name = tk.Label(master=frm_form, text="Last Name:")
-ent_last_name = tk.Entry(master=frm_form, width=50)
-# Place the widgets in the second row of the grid
-lbl_last_name.grid(row=1, column=0, sticky="e")
-ent_last_name.grid(row=1, column=1)
-
-# Create the Label and Entry widgets for "Address Line 1"
-lbl_address1 = tk.Label(master=frm_form, text="Address Line 1:")
-ent_address1 = tk.Entry(master=frm_form, width=50)
-# Place the widgets in the third row of the grid
-lbl_address1.grid(row=2, column=0, sticky="e")
-ent_address1.grid(row=2, column=1)
-
-# Create the Label and Entry widgets for "Address Line 2"
-lbl_address2 = tk.Label(master=frm_form, text="Address Line 2:")
-ent_address2 = tk.Entry(master=frm_form, width=5)
-# Place the widgets in the fourth row of the grid
-lbl_address2.grid(row=3, column=0, sticky=tk.E)
-ent_address2.grid(row=3, column=1)
-
-# Create the Label and Entry widgets for "City"
-lbl_city = tk.Label(master=frm_form, text="City:")
-ent_city = tk.Entry(master=frm_form, width=50)
-# Place the widgets in the fifth row of the grid
-lbl_city.grid(row=4, column=0, sticky=tk.E)
-ent_city.grid(row=4, column=1)
-
-# Create the Label and Entry widgets for "State/Province"
-lbl_state = tk.Label(master=frm_form, text="State/Province:")
-ent_state = tk.Entry(master=frm_form, width=50)
-# Place the widgets in the sixth row of the grid
-lbl_state.grid(row=5, column=0, sticky=tk.E)
-ent_state.grid(row=5, column=1)
-
-# Create the Label and Entry widgets for "Postal Code"
-lbl_postal_code = tk.Label(master=frm_form, text="Postal Code:")
-ent_postal_code = tk.Entry(master=frm_form, width=50)
-# Place the widgets in the seventh row of the grid
-lbl_postal_code.grid(row=6, column=0, sticky=tk.E)
-ent_postal_code.grid(row=6, column=1)
-
-# Create the Label and Entry widgets for "Country"
-lbl_country = tk.Label(master=frm_form, text="Country:")
-ent_country = tk.Entry(master=frm_form, width=50)
-# Place the widgets in the eight row of the grid
-lbl_country.grid(row=7, column=0, sticky=tk.E)
-ent_country.grid(row=7, column=1)
-
-# Create a new frame `frm_buttons` to contain the
-# Submit and Clear buttons. This frame fills the
-# whole window in the horizontal direction and has
-# 5 pixels of horizontal and vertical padding.
-frm_buttons = tk.Frame()
-frm_buttons.pack(fill=tk.X, ipadx=5, ipady=5)
-
-# Create the "Submit" button and pack it to the
-# right side of `frm_buttons`
-btn_submit = tk.Button(master=frm_buttons, text="Submit")
-btn_submit.pack(side=tk.RIGHT, padx=10, ipadx=10)
-
-# Create the "Clear" button and pack it to the
-# right side of `frm_buttons`
-btn_clear = tk.Button(master=frm_buttons, text="Clear")
-btn_clear.pack(side=tk.RIGHT, ipadx=10)
-
+# List of fields we want to have in our form
+fields = ["First Name:",
+          "Last Name:",
+          "Address Line1:",
+          "Address Line2:",
+          "City:",
+          "State/Province:",
+          "Postal Code:",
+          "Country:"]
+
+# The top frame contains the fields and their entry widgets
+top_frame = tk.Frame(relief=tk.SUNKEN, master = window)
+top_frame.pack()
+
+# We are going to use a 2 columns grid to place our widgets
+# We initialise the current row variable of our grid
+current_top_frame_row = 0
+
+for field in fields:
+    # For each field we create a label and entry widget
+    label = tk.Label(text=field, master=top_frame)
+    entry = tk.Entry(width=50, master=top_frame)
+    # We place them in the current row of our grid
+    label.grid(row=current_top_frame_row, column=0, sticky=tk.E)
+    entry.grid(row=current_top_frame_row, column=1)
+    # We increment the current line for the next field
+    current_top_frame_row += 1
+
+# Creating the bottom frame that contains
+# the Submit & Clear buttons
+bot_frame = tk.Frame(master=window)
+bot_frame.pack(fill=tk.X, ipadx=5, ipady=5)
+choices = ["Cancel",
+           "Submit"]
+
+for choice in choices:
+    # Create the button and pack it to the right side
+    button = tk.Button(text=choice, relief=tk.RAISED, master=bot_frame)
+    button.pack(side=tk.RIGHT, padx=5, ipadx=10)
+    
 # Start the application
 window.mainloop()