1. Imports and the data file

Lines 1–6

Every Python program starts by importing the tools it needs. This one needs four: json to store data, os to work with file paths, and tkinter together with ttk and messagebox to build the window and its dialogs. The data file's path is then defined once, as a constant, so the rest of the program uses the same value.

Code for this part

shop_manager.py, lines 1–6
1import json2import os3import tkinter as tk4from tkinter import ttk, messagebox5 6FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'shop_data.json')

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 1import json
Imports the standard json module. JSON is a text format for storing data. The module turns Python dictionaries and lists into text (json.dump) and turns that text back into Python objects (json.load). It ships with Python, so nothing has to be installed.
line 2import os
Imports os, which provides operating-system helpers. The program uses its os.path functions: line 6 builds the data file's path and line 19 checks whether the file exists.
line 3import tkinter as tk
Imports the standard GUI library tkinter under the short alias tk, so the code can write tk.Tk instead of tkinter.Tk.
line 4from tkinter import ttk, messagebox
Imports two submodules directly from tkinter: ttk, the themed widgets (labels, entry boxes, buttons, frames and the table), and messagebox, which shows small error and information dialogs. Because they are imported with from ... import, the code can write ttk.Button directly.
line 5
A blank line. Python ignores it; it only makes the code easier to read.
line 6FILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'shop_data.json')
Defines the constant FILE, the full path of the data file. Python has no real constants; an all-caps name is a convention that means “don't change this”. __file__ is the path of this script, abspath makes it absolute, dirname takes its folder, and os.path.join appends shop_data.json. The data file therefore always lives next to the script, whichever folder the program is started from.

Note

An earlier version used FILE = 'shop_data.json'. A relative path is resolved against the terminal's current folder, so starting the program from another folder showed no data and quietly created a new, empty file.

How it connects to the rest of the code

See also

json
line 16 writes the data with json.dump, line 22 reads it with json.load, and line 25 catches json.JSONDecodeError.
os
line 6 builds the path and line 19 checks that the file exists.
tk
line 66 creates the main window with tk.Tk(), and line 126 uses tk.END to clear the entry boxes.
ttk
Labels line 72, entry boxes line 73, the button frame line 131, the buttons line 134 and the table line 137.
messagebox
Error dialogs on line 89 and line 91, and the sales report on line 111.
FILE
Used three times: opening for writing line 15, checking it exists line 19 and opening for reading line 21.

Check your understanding

Try to answer each question before opening it.

  1. What would have to change if line 3 were just import tkinter, without as tk?

    Show answer

    Every tk. would become tkinter.: tkinter.Tk() on line 66 and tkinter.END on line 126.

  2. Why is FILE built from __file__ instead of being just 'shop_data.json'?

    Show answer

    A bare file name is looked up in the terminal's current folder. Building it from __file__ keeps the data file next to the script, wherever the program is started from.

  3. Which lines use messagebox, and for what?

    Show answer

    line 89 and line 91 show error dialogs; line 111 shows the sales report.

  4. Can FILE be changed later in the program?

    Show answer

    Yes. Python has no real constants; the capital letters are only a convention that says “don't change this”.