Which Tkinter Modules Were Renamed in Python 3

Which tkinter modules were renamed in Python 3?

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)

Difference between tkinter and Tkinter

It's simple.

For python2 it is:

from Tkinter import *

For python3 it is:

from tkinter import *

Here's the way how can you forget about this confusion once and for all:

try:
from Tkinter import *
except ImportError:
from tkinter import *

ImportError: No module named 'Tkinter'

You probably need to install it using something similar to the following:

  • For Ubuntu or other distros with Apt:

    sudo apt-get install python3-tk
  • For Fedora:

    sudo dnf install python3-tkinter

You can also mention a Python version number like this:


  • sudo apt-get install python3.7-tk

  • sudo dnf install python3-tkinter-3.6.6-1.fc28.x86_64

Finally, import tkinter (for Python 3) or Tkinter (for Python 2), or choose at runtime based on the version number of the Python interpreter (for compatibility with both):

import sys
if sys.version_info[0] == 3:
import tkinter as tk
else:
import Tkinter as tk

Tkinter tkFileDialog doesn't exist

That code would have worked fine in Python 2.x, but it is no longer valid. In Python 3.x, tkFileDialog was renamed to filedialog and placed inside the Tkinter package. Nowadays, you import it like so:

import tkinter.filedialog
# or
from tkinter import filedialog

Why does it say that no module named tkinter?

python 2 and python 3 use tkinter in a different way.

Note: Tkinter has been renamed to tkinter in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

The above lines are from python documentation. Not sure if python is loading tkinter using python 2 or python 3..May be internal PYTHONPATH is
messed up
Rather try this,

try:
import tkinter as tk
except ImportError:
import Tkinter as tk

Note: In these situations where you use multiple versions of same modules, try using virualenv

Virtual Env

Pyinstaller No Module Tkinter

I ended up finding the problem so figured I would share. As Bryan Oakley pointed out in the question comments, pyinstaller was using python3, I needed python2 since I was using the python2 Tkinter version (and also some other python2 modules).

I did pip uninstall pyinstaller and pip3 uninstall pyinstaller to make sure I had completely removed pyinstaller. I then did pip2.7 install pyinstaller to install it for python2. Pyinstaller then correctly used python2 and loaded the Tkinter module with no issues (well I did have some issues related to my python2 not being added to my path, but that was unrelated to this issue).

Tkinter module not found on Ubuntu

Since you mention synaptic I think you're on Ubuntu. You probably need to run update-python-modules to update your Tkinter module for Python 3.

EDIT: Running update-python-modules

First, make sure you have python-support installed:

sudo apt-get install python-support

Then, run update-python-modules with the -a option to rebuild all the modules:

sudo update-python-modules -a

I cannot guarantee all your modules will build though, since there are some API changes between Python 2 and Python 3.

ModuleNotFoundError: No module named 'Tkinter' error although it has always worked

Be careful, you must use tkinter for Python 3 and Tkinter for Python 2.

So, if you're using Python 3, the module has been renamed to tkinter.



Related Topics



Leave a reply



Submit