Updated 2017-03-02 21:51:17 by Programmingkid

Tkinter is the interface between Python and the Tk framework. Just because you may have upgraded your Tcl/Tk frameworks doesn't mean you will be able to use your new version of Tk in Python immediately. In this tutorial we are going to upgrade Tcl and Tk from 8.4.7 to 8.5.11. The benefits are more widgets available to your applications. Version 8.5.12 is the latest version that will build on Mac OS 10.4 easily, but it comes with a bug that crashes Python when you try to copy something from a Text widget. So I will show you how to build Tcl and Tk version 8.5.11. It will probably build on older and newer versions of Mac OS X as well.

Directions: edit

Tk and Tcl 8.5.11 Frameworks.zip

You may use this download to install these already compiled Tk and Tcl frameworks if you don't wish to try to compile them from source. You would just copy the included frameworks into the /Library/Frameworks folder. The rest of the tutorial can be skipped up to "Installing Python". These frameworks require Mac OS 10.4 or higher, with a PowerPC or x86 processor.

Download both the tcl and tk version 8.5.11 from these download links:

Open the terminal application.

Enter this command to make a new folder in your home folder: mkdir tcltk

Move the downloaded files into this folder. The home folder can be found on the sidebar. It is the home icon. Click it to display your home folder. The tcltk folder should be here.

Expand both files by simply double-clicking on them.

Execute this command in terminal: cd tcltk
(your terminal should be in the tcltk folder now)

In the terminal, enter these commands one at a time and push enter to execute each one:
(these commands build tcl and tk)

  • make -C tcl8.5.11/macosx
  • make -C tk8.5.11/macosx

(these commands install tcl and tk in the /Library/Frameworks/ folder. They will be called Tk.framework and Tcl.framework).

  • sudo make -C tcl8.5.11/macosx install
  • sudo make -C tk8.5.11/macosx install

If everything went to plan, you now have an upgraded tcl and tk frameworks in your /Library/Frameworks folder. Your Apple-supplied versions are untouched and still available. To check tcl, double-click the new Wish application located here: /Library/Frameworks/Tk.framework/Versions/8.5/Resources/Wish.app. This will open the tclsh program. You will see a %. Now enter 'puts $tcl_version'. If you see 8.5, you are using the newly installed tcl software. Good job.

Now we need to see if Tk 8.5 is working. Click on Wish's Application menu and select "About Tcl & Tk...". You will hopefully see something that says "Tcl 8.5.11 & Tk 8.5.11". If you did, it means you are doing great. The File menu has the "Run Widget Demo" menu item. I suggest you try it out. You will see a demonstration of all the widgets you have available to you.

At this point you may be tempted to run Python and see if Tkinter is using your new software. If you do, you will be disappointed at this point. Your Python was made to use the older version of Tk that came with Mac OS X. Now it is time to upgrade Python so it knows to use the new Tk framework.

Installing Python

I found the easiest way to install Python is to use TigerBrew. It is a package manager made for older Macs using Mac OS 10.4 and 10.5. To install it, start by downloading it from here https://github.com/mistydemeo/tigerbrew. You will see a page with files listed. Simply scroll down the page until you see "Installation". From there you will learn how to install and use TigerBrew. But for now, these commands will do:

  • export PATH=/usr/local/sbin:/usr/local/bin:$PATH
  • ruby -e "$(curl -fsSkL raw.github.com/mistydemeo/tigerbrew/go/install)"

Question: why not just make it so that TigerBrew can install Python and Tk for me?

Answer: It can currently install Python, but there is a problem with the Tcl-Tk package that prevents it from installing on Mac OS 10.4 (it tries to install version 8.6.6, which is not very compatible with Mac OS 10.4). The maintainer for the project has been notified of this issue. Hopefully a fix will be developed soon. If you are using Mac OS 10.5 or later, installing everything you need might be as simple as: brew install python --with-tcl-tk

Question: can I install Tk 8.6 on Mac OS 10.4?

Answer: Yes, but you would probably not like the result. That version would need X11 in order to work. It would be very unmac-like. The widgets wouldn't have the aqua look and feel that we all know. They would look pretty flat and uninteresting. Please see this page if you are still interested: https://github.com/mistydemeo/tigerbrew/issues/430.

In order to build Python so that it uses our new version of Tk, we have to make a few changes to the formula TigerBrew uses.

Question: Couldn't I just build Python from source?

Answer: Possibly, but I haven't succeeded that way. All my attempts have failed.

Steps to install Python (execute these in the terminal):

  • export EDITOR=/Applications/TextEdit.app/Contents/MacOS/TextEdit
  • brew edit python
  • Clear all the text in this file by simply selecting all the text and deleting it.
  • Download this file: http://www.mediafire.com/file/saprdo7d9f5jupn/replacement_python.rb
  • Copy and paste the text in the new formula into the TextEdit window.
  • Save the file.
  • Quit TextEdit.
  • Enter this command in the terminal and execute it: 'brew install python'.

After Python is done installing, you will want to test it out. Open a new terminal window and type 'Python'. The Python command should be running. You should see text like this: "Python 2.7.12 (default, ...". The date you built Python should be displayed after "default". Enter this code and try executing it:
from Tkinter import *
print TkVersion

You should see 8.5 printed on the screen. This means you have done it. You have successfully updated Tkinter!

Try this program out to see Tkinter in action:
from Tkinter import *
from ttk import *

root = Tk()
combobox = Combobox(root)
combobox['values'] = ("You did it!", "You are wonderful!", "Everyone loves you!")
combobox.set("You did it!")

To make sure this new version of python is used instead of the old version, we have to set the PATH variable to look in the right place first. To do this in terminal:

export EDITOR=/Applications/TextEdit.app/Contents/MacOS/TextEdit

open ~/.bashrc

If you see an error message stating this file does not exist, make a new file by opening TextEdit, then going to File->Save and enter the file name .bash_profile

Enter into TextEdit: export PATH=<path to python 2.7>:$PATH

Replace the <path to python 2.7> text with your python 2.7's actual path. For me this is "/usr/local/Cellar/python/2.7.12_3/bin"

Save the changes to a file in your home folder called .bashrc. TextEdit will tell you that files that begin with period will be invisible. Just click ' Use "." ' and continue.

If everything went well, you should be able to quit and open back up terminal and see your path with this command: echo $PATH

The key is the new python path should come before the old path (/usr/bin).

Conclusion: edit

You now have a better version of Tkinter to enjoy. If you want to contact me I can be reached here: [email protected]

Troubleshooting: edit

I suggest creating a new folder somewhere that will hold all the files. It is probably best to give this folder a simple name that has no spaces in it. It might also be a good idea to make sure the path to this folder doesn't have any spaces in it.

Be sure to have gcc installed. You can install it using Apple's Xcode installation or TigerBrew.

There might be software that may require updating or installing before Tcl or Tk can be built on your system. TigerBrew and google might be of use to you.