To get the most out of this book
This book expects that you know the basics of Python 3. You should know how to write and run simple scripts using built-in types and functions, how to define your own functions, and how to import modules from the standard library.
You can follow this book on a computer running a current version of Microsoft Windows, Apple macOS, or a distribution of GNU/Linux. Ensure that you have Python 3 and Tcl/Tk installed (Chapter 1, Introduction to Tkinter, contains instructions for Windows, macOS, and Linux) and that you have a code editing environment with which you are comfortable (we suggest IDLE since it comes with Python and uses Tkinter. We do not recommend the use of Jupyter, Spyder, or similar environments aimed at analytical Python rather than application development). In the later chapters, you'll need access to the internet so that you can install Python packages and the PostgreSQL database.
Download the example code files
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Python-GUI-Programming-with-Tkinter-2E. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Download the color images
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://static.packt-cdn.com/downloads/9781801815925_ColorImages.pdf.
Conventions used
There are a number of text conventions used throughout this book.
CodeInText
: Indicates code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles. For example: "Save the code in solve_the_worlds_problems.py
and execute it by typing python solve_the_worlds_problems.py
at a terminal prompt."
A block of code is set as follows:
import tkinter as tk
root = tk.TK()
def solve():
raise NotImplemented("Sorry!")
tk.Button(
root, text="Solve the world's problems", command=solve
).pack()
root.mainloop()
When we wish to draw your attention to a particular part of a code block, especially to indicate changes to existing code, the relevant lines or items are set in bold:
import tkinter as tk
from tkinter import messagebox
root = tk.TK()
def solve():
messagebox.showinfo('The answer?', 'Bananas?')
tk.Button(
root, text="Solve the world's problems", command=solve
).pack()
root.mainloop()
Note that all Python code in the book uses 2-space indents rather than the conventional 4-space indents.
Any command-line input or output is written with a $
indicating the prompt, as follows:
$ mkdir Bananas
$ cp plantains.txt Bananas/
Command line input intended for the Python shell or REPL is printed with a prompt of >>>
, like so:
>>> print('This should be run in a Python shell')
'This should be run in a Python shell'
Expected output from the shell is printed on a line with no prompt.
Bold: Indicates a new term, an important word, or words that you see on the screen, for example, in menus or dialog boxes. For example: "Select System info from the Administration panel."
Warnings or important notes appear like this.
Tips and tricks appear like this.
Executing Python and pip
When we need to instruct the reader to execute a Python script in this book, we indicate a command line such as the following:
$ python myscript.py
Depending on your operating system or Python configuration, the python
command may execute Python 2.x rather than Python 3.x. You can verify this by running the following command:
$ python --version
Python 3.9.7
If this command outputs Python 2 rather than 3 on your system, you will need to alter any python
commands so that your code is executed in Python 3. Typically, that means using the python3
command instead, like so:
$ python3 myscript.py
The same caveat applies to the pip
command used to install libraries from the Python Package Index. You may need to use the pip3
command instead to install libraries to your Python 3 environment, for example:
$ pip3 install --user requests