NameError: name 'tk' is not defined in Python

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Photo from Unsplash

NameError: name 'tk' is not defined in Python #

The Python "NameError: name 'tk' is not defined" occurs when we use the tkinter module without importing it first. To solve the error, import the tkinter module before using it - import tkinter as tk.

nameerror name tk is not defined

Here is an example of how the error occurs.

main.py
from tkinter import ttk # ⛔️ NameError: name 'tk' is not defined. Did you mean: 'ttk'? root = tk.Tk() frm = ttk.Frame(root, padding=10) frm.grid() ttk.Label(frm, text="Hello World!").grid(column=0, row=0) ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0) root.mainloop()

We have to import the tkinter module to solve the error.

main.py
# ✅ import tkinter as tk first import tkinter as tk from tkinter import ttk root = tk.Tk() frm = ttk.Frame(root, padding=10) frm.grid() ttk.Label(frm, text="Hello World!").grid(column=0, row=0) ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0) root.mainloop()

Even though the tkinter module is in the Python standard library, we still have to import it before using it.

Make sure you haven't used a capital letter t when importing tkinter because module names are case-sensitive.

Also, make sure you haven't imported tkinter in a nested scope, e.g. a function. Import the module at the top level to be able to use it throughout your code.

Note that in Python 3, tkinter is imported using a lowercase t, whereas in Python 2, it is imported with an uppercase T

An alternative to importing the entire tkinter module is to import only the classes and methods that your code uses.

main.py
from tkinter import Tk, ttk root = Tk() frm = ttk.Frame(root, padding=10) frm.grid() ttk.Label(frm, text="Hello World!").grid(column=0, row=0) ttk.Button(frm, text="Quit", command=root.destroy).grid(column=1, row=0) root.mainloop()

The example shows how to only import the Tk class and ttk module from tkinter.

Instead of accessing the members on the module, e.g. tk.Tk(), we now access them directly.

This should be your preferred approach because it makes your code easier to read.

For example, when we use an import such as import tkinter as tk, it is much harder to see which functions or classes from the tkinter module are being used in the file.

Conversely, when we import specific functions, it is much easier to see which classes from the tkinter module are being used.

You can view all of the classes and modules tkinter provides by visiting the official docs.

Conclusion #

The Python "NameError: name 'tk' is not defined" occurs when we use the tkinter module without importing it first. To solve the error, import the tkinter module before using it - import tkinter as tk.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.