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

  1. 1. Imports and the data file

    Lines 1–6

  2. 2. The Shop class: creating, saving and loading

    Lines 8–26

  3. 3. Adding, editing and deleting products

    Lines 28–48

  4. 4. Recording sales and the report

    Lines 50–63

  5. 5. The window and the input form

    Lines 65–75

  6. 6. Reading the form, run() and the button handlers

    Lines 77–111

  7. 7. Refreshing the table and selecting a row

    Lines 113–127

  8. 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.

ButtonViaHandlerClass methodSaves to file
Add productrunadd() line 93Shop.addyes
Edit productrunedit() line 96Shop.edityes
Delete productrundelete() line 99Shop.deleteyes
Record salerunsell() line 102Shop.sellyes
Searchrunsearch() line 105—no
Show allrunrefresh() line 113—no
Sales reportrunreport() line 109Shop.reportno

Index of names

Every name defined in the program, the line where it is defined and the lines that use it.

NameDefinedUsed on linesPage
FILE615, 19, 21Part 1
Shop865Part 2
Shop.save1434, 42, 48, 60Part 2
Shop.load1812Part 2
Shop.add2894Part 3
Shop.edit3697Part 3
Shop.delete44100Part 3
Shop.sell50103Part 4
Shop.report62110Part 4
shop6594, 97, 100, 103, 110, 116Part 5
root6667, 68, 72, 73, 131, 137, 143, 144, 146Part 5
fields7075, 78, 79, 80, 81, 100, 103, 106, 126, 127Part 5
values7794, 97Part 6
run84134Part 6
add93129Part 6
edit96129Part 6
delete99129Part 6
sell102130Part 6
search105130Part 6
report109130Part 6
refresh11387, 107, 130, 145Part 7
choose120142Part 7
buttons129133Part 8
frame131132, 134Part 8
table137114, 115, 118, 121, 124, 139, 140, 141, 142Part 8