[Tutor] Tkinter: no module named messagebox

Peter Otten __peter__ at web.de
Sat Aug 13 22:49:42 CEST 2011


brandon w wrote:

> I have tried to follow the tutorial I found here:
> 
> Python 2.7 Tutorial
> http://www.youtube.com/watch?v=uh6AdDX7K7U
> 
> This is what I have done so far:
> 
> #!/usr/bin/python
> 
> from Tkinter import *
> import Tkinter.MessageBox
> 
> myapp = Tk()
> myapp.title("This is the gui title")
> myapp.geometry("500x500+600+600")
> myapp.mainloop()
> 
> I run it like this:
> 
> $ python tktest.py
> 
> I am getting the error message:
> 
> Traceback (most recent call last):
>    File "tkwindow.py", line 12, in <module>
>      import Tkinter.MessageBox
> ImportError: No module named MessageBox
> 
> And then after changing the uppercase to lowercase this:
> 
> Traceback (most recent call last):
>    File "tkwindow.py", line 12, in <module>
>      import Tkinter.messagebox
> ImportError: No module named messagebox
> 
> 
> How do I find the modules in Tkinter?

The simplest approach is probably to explore your file system:

Step 1: where's Tkinter?

$ python -c 'import Tkinter, os; print os.path.dirname(Tkinter.__file__)'
/usr/lib/python2.6/lib-tk

Step 2: explore the neighbourhood

$ find `python -c 'import Tkinter, os; print 
os.path.dirname(Tkinter.__file__)'` -iname '*messagebox*.py'
/usr/lib/python2.6/lib-tk/tkMessageBox.py

Step 3: we have a candidate; let's verify:

$ python
<snip>
>>> import tkMessageBox
>>> dir(tkMessageBox)
['ABORT', 'ABORTRETRYIGNORE', 'CANCEL', 'Dialog', 'ERROR', 'IGNORE', 'INFO', 
'Message', 'NO', 'OK', 'OKCANCEL', 'QUESTION', 'RETRY', 'RETRYCANCEL', 
'WARNING', 'YES', 'YESNO', 'YESNOCANCEL', '__builtins__', '__doc__', 
'__file__', '__name__', '__package__', '_show', 'askokcancel', 
'askquestion', 'askretrycancel', 'askyesno', 'askyesnocancel', 'showerror', 
'showinfo', 'showwarning']
>>> tkMessageBox.askyesno("does that help you?")
True

Of course you could also consult some documentation. I usually google for 
tkinter new mexico.
http://infohost.nmt.edu/tcc/help/pubs/tkinter/dialogs.html#tkMessageBox

> I am running Python 2.6.6. It may be a little older and not have the
> messagebox module.

I think tkMessageBox has been moved to tkinter.messagebox in Python 3.



More information about the Tutor mailing list