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¶
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 1
import json¶ - Imports the standard
jsonmodule. 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 2
import os¶ - Imports
os, which provides operating-system helpers. The program uses itsos.pathfunctions: line 6 builds the data file's path and line 19 checks whether the file exists. - line 3
import tkinter as tk¶ - Imports the standard GUI library
tkinterunder the short aliastk, so the code can writetk.Tkinstead oftkinter.Tk. - line 4
from tkinter import ttk, messagebox¶ - Imports two submodules directly from
tkinter:ttk, the themed widgets (labels, entry boxes, buttons, frames and the table), andmessagebox, which shows small error and information dialogs. Because they are imported withfrom ... import, the code can writettk.Buttondirectly. - line 6
FILE = 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,abspathmakes it absolute,dirnametakes its folder, andos.path.joinappendsshop_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 withjson.load, and line 25 catchesjson.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 usestk.ENDto 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.
What would have to change if line 3 were just
import tkinter, withoutas tk?Why is
FILEbuilt 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.Which lines use
messagebox, and for what?Can
FILEbe 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”.