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?

Asked By: Tom Lowbridge

||

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

Answered By: Billal Begueradj

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 :))

Answered By: Hung Truong

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.")
Answered By: Poikilos

for python 3.x

import tkinter

import tkinter.messagebox

Answered By: Suraj Verma

from tkinter import messagebox sous Python 3
messagebox.showinfo(title=None, message=None, **options)

Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.