Question

I am trying to create a file chooser dialog box. However, when I try to import tkMessageBox in Python 3, I get an error claiming that the module does not exist.

import tkMessageBox
# ImportError: No module named 'tkMessageBox' 

I get similar errors when trying to import other Tkinter modules in Python 3.

import Tkinter          # No module named 'Tkinter'
import tkColorChooser   # No module named 'tkColorChooser'
import tkFileDialog     # No module named 'tkFileDialog'

How do I import Tkinter modules in Python 3? What are the new module names?

Was it helpful?

Solution

The Tkinter package from Python 2 has been renamed to tkinter in Python 3, as well as other modules related to it.

Here is a list of renamed modules:

  • Tkintertkinter
  • tkMessageBoxtkinter.messagebox
  • tkColorChoosertkinter.colorchooser
  • tkFileDialogtkinter.filedialog
  • tkCommonDialogtkinter.commondialog
  • tkSimpleDialogtkinter.simpledialog
  • tkFonttkinter.font
  • Tkdndtkinter.dnd
  • ScrolledTexttkinter.scrolledtext
  • Tixtkinter.tix
  • ttktkinter.ttk

I advise you to learn how to dynamically browse the modules with the dir command. If you are under windows, configure Python to use readline module to get auto-completion and make it much easier to list available classes in a module.

For a description of each module, refer to the official Python documentation. (Tkinter in Python 2.x, tkinter in Python 3.x)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top