We will write all our projects using Python Version 3.6.3, which is the latest stable release of Python at the time of writing.
The Python download package and instructions for downloading for different platforms are available at https://www.python.org/downloads/release/python-363/.
The installer binaries for macOS X and the Windows platform are available at the aforementioned link.
If you are following along on Unix, Linux, or BSD, the following procedure will install Python from the source.
First, install tk8.6-dev and python3-tk packages on your computer using your applicable package manager. For instance, on Debian-based systems such as Ubuntu and Mint, run the following two commands from the Terminal:
sudo apt install tk8.6-dev
sudo apt install python3-tk
Download Python 3.6.3 from the preceding link and extract it to any location of your choice. Open a Terminal in the location where you extracted Python and type in the following commands:
./configure
make
make test
sudo make altinstall
This should install Python 3.6.3 on your computer. Now open a command line and enter the following command:
$ python3.6
This will open the Python 3.6 interactive shell. Type in the following command:
>>> import tkinter
This command should execute without any errors. If there are no error messages, the Tkinter module is installed on your Python distribution.
When working with examples from this book, we do not support any Python Version except for Python 3.6.3, which comes bundled with Tkinter Tcl/Tk Version 8.6. However, most of the examples should work out-of-the-box on other minor Python 3 Versions.
To check whether you have the correct Tkinter Version on your Python installation, type the following commands in your IDLE or interactive shell:
>>> import tkinter
>>> tkinter._test()
This should confirm the Tcl/Tk Version as 8.6. We are now ready to build our GUI programs!
The next steps are optional and you may skip them at your discretion. While the preceding steps are sufficient for us to develop our programs, I highly recommend that you use a virtual environment for developing your programs.
Virtual environments provide a secluded environment with no conflicts with system programs, and they can be easily reproduced on any other system.
So now let's set up a virtual environment. First, create a folder where you will keep all projects from this book. Let's call it myTkinterProjects or whatever suits you.
Next, find the location of the Python 3.6 installation on your computer. On my computer, I can find the location of the Python installation by running the following command:
$ which python3.6
Take a note of the location. For me it is /usr/local/bin/python3.6. Now open a Terminal in your myTkinterProjects folder and run the following command:
$ virtualenv -p /location/of /python3.6 myvenv/
This will create a new virtual environment in a folder named myvenv inside your project folder.
Lastly, we need to activate this virtual environment. This is done by running the following command:
$ source myenv/bin/activate
Now if you type the command python, it should pick up Python 3.6.3 from within your virtual environment.
From now onward, every time we have to run a Python script or install a new module, we will first activate the virtual environment using the preceding command and run or install the module within this new virtual environment.