Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which tkinter modules were renamed in Python 3?

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?

like image 464
Pratik Deoghare Avatar asked Mar 23 '09 12:03

Pratik Deoghare


People also ask

What is the difference between from tkinter import * and import tkinter as TK?

Using import tkinter as tkIt comes with all the modules defined in tkinter. However, to save the major typing efforts, we import the tkinter library with some acronym further that can be used to create an instance of widgets. Thus, the application structures become more aesthetical by using the import tkinter as tk.

Is _tkinter the same as tkinter?

The tkinter package (“Tk interface”) is the standard Python interface to the Tcl/Tk GUI toolkit. Both Tk and tkinter are available on most Unix platforms, including macOS, as well as on Windows systems.


2 Answers

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)

like image 81
Mapad Avatar answered Oct 22 '22 07:10

Mapad


Quick Script for using Tkinter / tkinter for Python 2. & Python 3.**

I had a script which had different imports of Python 2.* Tkinter so browsing a but I see that the answer are all scattered. Here a small summary with a safe Script for using both Python versions.

try:  # Python 2.7
    import Tkinter as tk
    import tkColorChooser as color
    import tkCommonDialog as cdialog
    import Tkconstants as const
    import Tkdnd as dnd
    import tkFileDialog as fdialog
    import tkFont as font
    import tkMessageBox as msgbox
    import ScrolledText as stext
    import tkSimpleDialog as sdialog
    import Tix as tix
    import ttk

except ImportError:  # Python 3.* 
    import tkinter as tk
    from tkinter import (
        colorchooser as color,
        commondialog as cdialog,
        constants as const,
        dialog,
        dnd,
        filedialog as fdialog,
        font,
        messagebox as msgbox,
        scrolledtext as stext,
        simpledialog as sdialog,
        tix,
        ttk
    )

List of ModuleNotFoundError Errors (When Running Python 3.)

ModuleNotFoundError: No module named 'Tkinter'
ModuleNotFoundError: No module named 'tkMessageBox'
ModuleNotFoundError: No module named 'ScrolledText'
ModuleNotFoundError: No module named 'tkFileDialog'
like image 23
Federico Baù Avatar answered Oct 22 '22 07:10

Federico Baù