5. The window and the input form¶
Lines 65–75
From here on the code leaves the class and builds the user interface. First the shop object and the main window are created, then a loop builds four rows of label and entry box: name, purchase price, sale price and quantity.
Code for this part¶
65shop = Shop()66root = tk.Tk()67root.title('مدیریت فروشگاه - Python')68root.geometry('760x560')69 70fields = {}71for row, (key, label) in enumerate([('name', 'نام کالا'), ('buy', 'قیمت خرید'), ('sell', 'قیمت فروش'), ('qty', 'تعداد')]):72 ttk.Label(root, text=label).grid(row=row, column=0, padx=8, pady=5, sticky='e')73 entry = ttk.Entry(root, width=28)74 entry.grid(row=row, column=1, padx=8, pady=5)75 fields[key] = entry
Click a line number to jump to its explanation; click “line …” in an explanation to highlight that line in the code.
Line by line¶
- line 65
shop = Shop()¶ - Creates a
Shopobject. This runs__init__(line 9) and loads the data from disk.shopis a global variable used by every function further down the file. - line 66
root = tk.Tk()¶ - Creates the program's main window. A tkinter program has exactly one
Tk(), and every other widget lives inside it. - line 68
root.geometry('760x560')¶ - The initial window size: 760 pixels wide by 560 high. The string format is
'WIDTHxHEIGHT'. - line 70
fields = {}¶ - An empty dictionary that will hold the entry boxes under the keys
name,buy,sellandqty, so the name box can later be reached withfields['name']. - line 71
for row, (key, label) in enumerate([('name', 'نام کالا'), ('buy', 'قیمت خرید'), ('sell', 'قیمت فروش'), ('qty', 'تعداد')]):¶ - Loops over a list of four (key, label) tuples.
enumeratenumbers them 0 to 3 intorow, and(key, label)unpacks each tuple. On the first passrowis0,keyis'name'andlabelis the product-name label. - line 72
ttk.Label(root, text=label).grid(row=row, column=0, padx=8, pady=5, sticky='e')¶ - Creates a text label and immediately places it with
gridat rowrow, column 0.padxandpadyadd space around it, andsticky='e'pins it to the east (right) side of its cell. The label is never needed again, so it is not stored. - line 73
entry = ttk.Entry(root, width=28)¶ - Creates an entry box 28 characters wide and keeps it in
entry.
How it connects to the rest of the code¶
See also
shop- Used in
addline 94,editline 97,deleteline 100,sellline 103,reportline 110 andrefreshline 116. root- Parent of every widget: labels line 72, entry boxes line 73, the button frame line 131, the table line 137. Resizing is set up on line 143 and line 144, and the main loop starts on line 146.
fields- Read in
valuesline 78–line 81,deleteline 100,sellline 103 andsearchline 106; written inchooseline 126 and line 127.
Check your understanding¶
Try to answer each question before opening it.
Why are line 73 and line 74 two lines instead of
entry = ttk.Entry(...).grid(...)?Show answer
gridreturnsNone, soentrywould beNoneand its text could never be read.Which values does
rowtake in the loop on line 71?Show answer
0, 1, 2 and 3 — one per form field, from
enumerate.What does
sticky='e'do on line 72?Show answer
It pins the label to the east (right) side of its grid cell.
When does
Shop.__init__run?Show answer
Once, on line 65, when
shop = Shop()creates the object.