TkMessageBox – No Module
Question:
import TkMessageBox
When I import TkMessageBox it displays the messsge ‘ImportError: No module named ‘TkMessageBox’.
As far as I know im using python 3.3.2 and Tk 8.5.
Am I using the wrong version of python or importing it wrong ?
Any answers would be extremely useful. Alternatively is there something similar in the version i am using?
Answers:
In Python3.x things have changed a little bit:
>>> import tkinter
>>> import tkinter.messagebox
>>>
I mean what we call tkMessageBox
in Python2.x becomes tkinter.messagebox
in Python3.x
In Python 2.x, to import, you’d say import tkMessageBox
. But in Python 3.x, it’s been renamed to import tkinter.messagebox
.
Hope it helped :))
If you don’t want to have to change the code for Python 2 vs Python 3, you can use import as:
try:
from tkinter import messagebox
except ImportError:
# Python 2
import tkMessageBox as messagebox
:edit:
However, tkinter is in a separate package in Debian due to Debian policies, so to avoid an incorrect fallback to Python 2 code I now use:
import sys
if sys.version_info.major >= 3:
from tkinter import messagebox
else:
import tkMessageBox as messagebox
Then using messagebox as follows will work in either version:
messagebox.showerror("Error", "Message.")
for python 3.x
import tkinter
import tkinter.messagebox
from tkinter import messagebox sous Python 3
messagebox.showinfo(title=None, message=None, **options)
import TkMessageBox
When I import TkMessageBox it displays the messsge ‘ImportError: No module named ‘TkMessageBox’.
As far as I know im using python 3.3.2 and Tk 8.5.
Am I using the wrong version of python or importing it wrong ?
Any answers would be extremely useful. Alternatively is there something similar in the version i am using?
In Python3.x things have changed a little bit:
>>> import tkinter
>>> import tkinter.messagebox
>>>
I mean what we call tkMessageBox
in Python2.x becomes tkinter.messagebox
in Python3.x
In Python 2.x, to import, you’d say import tkMessageBox
. But in Python 3.x, it’s been renamed to import tkinter.messagebox
.
Hope it helped :))
If you don’t want to have to change the code for Python 2 vs Python 3, you can use import as:
try:
from tkinter import messagebox
except ImportError:
# Python 2
import tkMessageBox as messagebox
:edit:
However, tkinter is in a separate package in Debian due to Debian policies, so to avoid an incorrect fallback to Python 2 code I now use:
import sys
if sys.version_info.major >= 3:
from tkinter import messagebox
else:
import tkMessageBox as messagebox
Then using messagebox as follows will work in either version:
messagebox.showerror("Error", "Message.")
for python 3.x
import tkinter
import tkinter.messagebox
from tkinter import messagebox sous Python 3
messagebox.showinfo(title=None, message=None, **options)