How to Import Tkinter Modules

Examples of how to import Python tkinter modules.

Updated

Table of Contents

Import Tkinter Modules


Tkinter Modules


When we use tkinter, we also use a number of additional Python modules.

You can see these modules in the source code on GitHub. The main code for the tkinter module itself is in the __init__.py file.

List of available modules:

tkinter
tkinter.colorchooser
tkinter.commondialog
tkinter.constants
tkinter.dialog
tkinter.dnd
tkinter.filedialog
tkinter.font
tkinter.messagebox
tkinter.scrolledtext
tkinter.simpledialog
tkinter.tix
tkinter.ttk

Import Modules


We can import tkinter like other Python libraries using import <module>.

import tkinter
import tkinter.ttk

root = tkinter.Tk()

button = tkinter.ttk.Button(root, text="Button")
button.pack()

root.mainloop()

It may be more convenient to use import <module> as <alias>.

The alias can be a short and unique name.

import tkinter as tk
import tkinter.ttk as ttk

root = tk.Tk()

button = ttk.Button(root, text="Button")
button.pack()

root.mainloop()

We can import additional modules as they are.

import tkinter.ttk
import tkinter.filedialog
import tkinter.font
import tkinter.messagebox
# or
from tkinter import ttk
from tkinter import filedialog
from tkinter import font
from tkinter import messagebox

Or use aliases as well.

import tkinter.filedialog as tk_filedialog
import tkinter.font as tk_font
import tkinter.messagebox as tk_messagebox
# or
from tkinter import filedialog as tk_filedialog
from tkinter import font as tk_font
from tkinter import messagebox as tk_messagebox

We can also import a specific class or function from a module.

from tkinter.filedialog import askopenfilename
from tkinter.font import Font, families
from tkinter.messagebox import showinfo, askyesno

Constants Module


We don't need to import the tkinter.constants module as it has already been imported via tkinter init file.

- import certain constants

>>> from tkinter import LEFT, SUNKEN
>>> LEFT
'left'
>>> SUNKEN
'sunken'
>>>

- use constants as module attributes

>>> import tkinter
>>> tkinter.LEFT
'left'
>>> tkinter.SUNKEN
'sunken'
>>>

- use alias and use constants as module attributes

>>> import tkinter as tk
>>> tk.LEFT
'left'
>>> tk.SUNKEN
'sunken'
>>>

Import Issues


Old Python code


When we try to import some tkinter modules, we may get an error:

import Tkinter
No module named 'Tkinter'

Or with other modules:

import tkFont
No module named 'tkFont'

The above imports are specific to Python 2. The standard library has been reorganized, and some modules have been renamed for Python 3. You may be trying to run Python 2 code using Python 3.

Reorganized and renamed modules
Old New
Tkintertkinter
tkColorChoosertkinter.colorchooser
tkCommonDialogtkinter.commondialog
Tkconstantstkinter.constants
Dialogtkinter.dialog
Tkdndtkinter.dnd
FileDialog, tkFileDialogtkinter.filedialog
tkFonttkinter.font
tkMessageBoxtkinter.messagebox
ScrolledTexttkinter.scrolledtext
SimpleDialog, tkSimpleDialogtkinter.simpledialog
Tixtkinter.tix
ttktkinter.ttk

Import for Python 2 and Python 3


Tkinter code written for Python 2 can also be used in newer versions of Python. We can try importing renamed tkinter modules using aliases.

try:
    import Tkinter # Python 2
except:
    import tkinter as Tkinter # Python 3

root = Tkinter.Tk()

button = Tkinter.Button(root, text="Button")
button.pack()

root.mainloop()

No module named 'tkinter'


When we try to import tkinter, we can get an error:

No module named 'tkinter'

Tkinter is a Python interface to the Tk library for building cross-platform GUIs. It relies on Tcl/Tk being installed on your system. On some platforms, we may need to install Tcl/Tk as well.

See: Installing Tk


Wildcard Import


Wildcard import can cause an error, the reason of which is not obvious.

Wildcard import example:

from <module> import *

This imports whatever names are defined in the module into the current namespace. Classes, functions, module variables, and names imported into this module from other Python files.


An example of an error when using the widget option.

The tkinter.ttk module contains several classes whose names are the same as those in tkinter.

Both modules have 12 widget classes with the same names: Button, Checkbutton, Entry, Frame, Label, LabelFrame, Menubutton, PanedWindow, Radiobutton, Scale, Scrollbar, and Spinbox. Six classes are new in ttk: Combobox, Notebook, Progressbar, Separator, Sizegrip, and Treeview.

Button class is present in both modules. The difference lies in the appearance and widget options that we can use with the buttons.

What is this button in the code, tkinter.Button or tkinter.ttk.Button?

Code:
from tkinter import *
from tkinter.ttk import *

# the Tk class is only found in the tkinter module
root = Tk()

# background:
# We are using the option that is suitable for tkinter.Button,
# but not suitable for tkinter.ttk.Button.
button = Button(root, text="Button",
                background="white")
button.pack()

# this class is only in the tkinter.ttk module
combobox = Combobox(root, values=["A", "B"])
combobox.pack()

root.mainloop()
Output:
Traceback (most recent call last):
  ...
_tkinter.TclError: unknown option "-background"

If tkinter.ttk import follows tkinter import, it overrides the tkinter widgets with the same class names. In this case, it's the tkinter.ttk.Button. Style properties such as the background color for this button are set differently.

If we swap imports in the above example, then the code will work. But it's still better to explicitly specify which widgets we want to use. For example: import tkinter and use tkinter.Button(), or import tkinter as tk and use tk.Button().


Reverse example using a class method. tkinter import overrides ttk widgets.

Code:
from tkinter.ttk import *
from tkinter import *

# the Tk class is only found in the tkinter module
root = Tk()

button = Button(root, text="Button")
button.pack()

# instate:
# We are using the method that is suitable for tkinter.ttk.Button,
# but not suitable for tkinter.Button.
if button.instate(["!disabled"]):
    print("Button is not disabled.")

root.mainloop()
Output:
Traceback (most recent call last):
  ...
AttributeError: 'Button' object has no attribute 'instate'

In this case, it's the tkinter.Button. This button has no such method.


If necessary, we can check the class, options, and methods of the widget.

from tkinter import *
from tkinter.ttk import *

root = Tk()

button = Button(root, text="Button")
button.pack()

# which button widget
print(Button)
print(button.__class__)
# widget options
print(button.keys())
# data attributes and methods
print(dir(button))

root.mainloop()

Popular posts from this blog

Tkinter Button Widget