Join Barron Stone for an in-depth discussion in this video Installing Python 3 and Tcl/Tk for Mac, part of Python GUI Development with Tkinter.
The Mac operating system comes pre-installed with Python. However, the default Mac Python installation may not include the correct version of Tcl/Tk to use TkInter. Also, the Mac OS typically includes Python 2, whereas this course is oriented around using Python 3. In this video, I'll walk you through the entire installation process for Python 3 and Tcl/Tk on Mac to ensure you have the most up-to-date software. I'll begin with downloading and installing Python 3.4, then I'll show you how to get the correct version of Tcl/Tk for your operating system.
To get the Python installer, go to the Python download page at python dot org slash download. Scroll down and select the version that you want to use for your computer. In this case, I want to use Python 3.4 for Mac OS 10.6 and later. Click on the link to start the download. Once the download is completed, you can navigate to your Downloads and double-click on the disk image file. This will mount the image and when it opens, you can control-click on the dot mpkg file.
This will give you a menu where you can choose to open the file. Selecting Open will bring up the Python Installer. Click Continue to progress through the menus. Eventually it will prompt you to agree to the software license agreement. Click the Agree button. And then you'll see an Install button. When you click on this Install button, it will prompt you for your administrative password. Enter the password. And then click the Install Software button. After the Installer finishes, we'll have Python 3.4 on this machine.
However, we still need to download and install Tcl/Tk. If we look back at that Python download page, you notice there's a little footnote marker next to our download link. If I click that, it will take me to a footnote at the bottom of the page which says there is important information about IDLE, Tklnter and Tcl/Tk on MAC OS 10 here. Follow that link and it will take you to a page that tells us we need to install a version of Tcl/Tk 8.5. There's a table down below which we can use to determine which version we'll need, based on our version of the Mac OS.
Since I'm using 10.9 and I'm using Python Release 3.4.1, I'll need ActiveTcl 8.5.15. If I click on that link, it will take me to a lower section of the webpage where I can find the link to ActiveState's website. Follow that link. ActiveState provides up-to-date binary distributions of Tcl/Tk. Scoll down on the page to find the distribution that matches the version we saw on the previous chart. I'm selecting this link because I need version 8.5.15.
And I'm using Mac OS 10.5 or greater. Click on the link to start the download. And when the download is completed, you can navigate to your Downloads, and double-click on the image file to mount the disk image. Again, we'll use our control-click to select the pkg file, and choose Open from the menu. This brings up the installer for ActiveState's Active Tkl 8.5, which is very similar to the one we just went through for Python 3.4, Click the Continue button to navigate the menus.
Select Agree when you are prompted to agree to the software license agreement. And when you see the Install button, click that. And again, you will be promted to enter your username and password. Enter the credentials and then hit the Install button. After the ActiveTcl installation is completed, we can eject those disk images because we're finished with them. Go to Finder, eject Python 3.4.1 and eject ActiveTcl.
Now that we have Python 3.4 and ActiveTcl 8.5.15 installed, to verify that everything is working correctly, let's go to Finder and open IDLE. IDLE is the integrated development environment which is included with Python. You can use IDLE to perform the following checks on your machine, or you can run the Python 3 interpreter from another shell program of your choice. First, let's try importing the TkInter package by typing import tkinter into the Python command prompt and pressing Enter.
Notice that both import and tkInter are spelled in all lower-case letters, because we're using Python 3. If we did not get any error messages from this, then it means that Python successfully found and imported the TkInter package. Next, type import underscore tkInter. This will import the compiled binary associated with the TkInter package, and press Enter. Now that we have successfully imported the TkInter package and the associated binaries, let's run the included test routine.
Type tkInter dot underscore test, open parenthesis, close parenthesis, and press Enter. You should see a pop-up window like the one shown here. If you click on the Click Me button, it should add square brackets around the text, and the window will also tell you what version of Tck/Tk you are using. You can press the Quit button to close out the window. If all of that runs without error, then you are good to go with TkInter.
One last thing I want to point out is that IDLE is actually written in Python using the TkInter package. The little rocket ship icon that appears down in the dock is actually the Tk icon. This means that just by virtue of successfully opening IDLE, we already know that TkInter is working on this machine. However, as I mentioned before, you can still perform these tests for TkInter without IDLE by running the Python interpreter from another shell. If you are running Python from another terminal program, be sure that you are using Python Version Three instead of Python Version Two.
Otherwise, these TkInter commands will not work.
- Installing Python 3 and Tcl/Tk for Mac or Windows
- Creating and configuring themed Tk widgets
- Decorating the GUI with text labels and images
- Capturing input from buttons, menus, and entry fields
- Presenting choices with check boxes and radio buttons
- Using geometry managers to lay out the GUI
- Organizing widgets inside of frames and windows
- Handling user actions with event-driven programming
- Creating a simple drawing tool with the Canvas
- Prompting users with pop-up dialog boxes