Python Learning¶
This guide explains a complete Python program — a small shop manager — line by line. The program uses tkinter to open a window where you can add, edit and delete products, record sales and see a sales report. Everything is saved to shop_data.json.
The 146-line source is split into eight parts. Each part shows its code first, then explains every line, then lists where its names are used elsewhere, and ends with practice questions.
Contents¶
Lines 1–6
2. The Shop class: creating, saving and loading
Lines 8–26
3. Adding, editing and deleting products
Lines 28–48
4. Recording sales and the report
Lines 50–63
5. The window and the input form
Lines 65–75
6. Reading the form, run() and the button handlers
Lines 77–111
7. Refreshing the table and selecting a row
Lines 113–127
8. Buttons, the table and starting the program
Lines 129–146
What each button does¶
Every button follows the same path: click → run → button handler → Shop method → save to file → refresh the table.
| Button | Via | Handler | Class method | Saves to file |
|---|---|---|---|---|
| Add product | run | add() line 93 | Shop.add | yes |
| Edit product | run | edit() line 96 | Shop.edit | yes |
| Delete product | run | delete() line 99 | Shop.delete | yes |
| Record sale | run | sell() line 102 | Shop.sell | yes |
| Search | run | search() line 105 | — | no |
| Show all | run | refresh() line 113 | — | no |
| Sales report | run | report() line 109 | Shop.report | no |
Index of names¶
Every name defined in the program, the line where it is defined and the lines that use it.
| Name | Defined | Used on lines | Page |
|---|---|---|---|
FILE | 6 | 15, 19, 21 | Part 1 |
Shop | 8 | 65 | Part 2 |
Shop.save | 14 | 34, 42, 48, 60 | Part 2 |
Shop.load | 18 | 12 | Part 2 |
Shop.add | 28 | 94 | Part 3 |
Shop.edit | 36 | 97 | Part 3 |
Shop.delete | 44 | 100 | Part 3 |
Shop.sell | 50 | 103 | Part 4 |
Shop.report | 62 | 110 | Part 4 |
shop | 65 | 94, 97, 100, 103, 110, 116 | Part 5 |
root | 66 | 67, 68, 72, 73, 131, 137, 143, 144, 146 | Part 5 |
fields | 70 | 75, 78, 79, 80, 81, 100, 103, 106, 126, 127 | Part 5 |
values | 77 | 94, 97 | Part 6 |
run | 84 | 134 | Part 6 |
add | 93 | 129 | Part 6 |
edit | 96 | 129 | Part 6 |
delete | 99 | 129 | Part 6 |
sell | 102 | 130 | Part 6 |
search | 105 | 130 | Part 6 |
report | 109 | 130 | Part 6 |
refresh | 113 | 87, 107, 130, 145 | Part 7 |
choose | 120 | 142 | Part 7 |
buttons | 129 | 133 | Part 8 |
frame | 131 | 132, 134 | Part 8 |
table | 137 | 114, 115, 118, 121, 124, 139, 140, 141, 142 | Part 8 |