[Tutor] Problems with Python 3 and Tkinter

Peter Anderson peter.anderson at internode.on.net
Sat Feb 21 13:00:28 CET 2009


I am trying to learn Python and have just installed Python 3. I am going 
back over some previous scripts trying to convert them to run under 
Python 3 (and hopefully learn a bit more in the process). I am having 
great problems with scripts that use Tkinter. A typical example is a 
script called calculator.py (the full script can be viewed at:

http://openbookproject.net/pybiblio/tips/wilson/tkinterhelp.php )

I have shown some of the problem lines from the script below.

01 from Tkinter import *
02 from tkMessageBox import showerror
03 from math import sqrt
...
41 except ValueError:
42 if self.number.get() == '':
43 showerror('Entry error', 'You have to put a number in the blank.')
44 else:
45 showerror('Square root error',
46 "Can't find the square root of '%s'" % self.number.get())
...

When I run the calculator.py script I get the following error message:

---------- Python 3 ----------
Traceback (most recent call last):
File "calculator.py", line 1, in <module>
from Tkinter import *
ImportError: No module named Tkinter

Output completed (0 sec consumed)


If I change line 01 as follows:

01 from tkinter import *

I get the following error message:

---------- Python 3 ----------
Traceback (most recent call last):
File "calculator.py", line 2, in <module>
from tkMessageBox import showerror
ImportError: No module named tkMessageBox

Output completed (0 sec consumed)

Clearly Python 3 needs the Tkinter library typed in lower case. Then it 
passes on to line 02 where it can't find the module tkMessageBox. I have 
a copy of Grayson's "Python and Tkinter Programming" and the original 
script above seems to be Ok.

If I comment out line 02

01 from tkinter import *
02 # from tkMessageBox import showerror

The script now runs Ok unless I try to calculate a square root with no 
value entered in the input field. Note: Lines 41 to 46 are there to 
catch such errant behaviour. With no input I get the following error 
message:

---------- Python 3 ----------
Exception in Tkinter callback
Traceback (most recent call last):
File "calculator.py", line 39, in calc
(self.number.get(), sqrt(float(self.number.get()))) )
ValueError: empty string for float()

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Python30\lib\tkinter\__init__.py", line 1399, in __call__
return self.func(*args)
File "calculator.py", line 43, in calc
showerror('Entry error', 'You have to put a number in the blank.')
NameError: global name 'showerror' is not defined


As part of my 'problem resolution' process I have run scripts under 
Python 3 that just import the tkinter library and they run fine. It only 
when I try to use modules such as tkMessageBox that I get problems.

I have spent hours researching and Googling this problem. Is there 
anyone who can help me understand how to get this script running. 
Additionally, is there any references to Python 3 and Tkinter other than 
the standard documentation (which is flimsy at best).

Regards,
Peter
-- 
*Peter Anderson*
There is nothing more difficult to take in hand, more perilous to 
conduct, or more uncertain in its success, than to take the lead in the 
introduction of a new order of things—Niccolo Machiavelli, /The Prince/, 
ch. 6


More information about the Tutor mailing list