Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError when importing Tkinter in Python [duplicate]

Tags:

python

tkinter

I'm trying to test GUI code using Python 3.2 with standard library Tkinter but I can't import the library.

This is my test code:

from Tkinter import *

root = Tk()
w = Label(root, text="Hello, world!")
w.pack()
root.mainloop()

The shell reports this error:

Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
from Tkinter import *
ImportError: No module named Tkinter
like image 739
Carry All Avatar asked Sep 21 '11 11:09

Carry All


People also ask

Why we use from tkinter import * in Python?

The significance of "import *" represents all the functions and built-in modules in the tkinter library. By importing all the functions and methods, we can use the inbuilt functions or methods in a particular application without importing them implicitly.

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

Using import tkinter as tk It 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.

What is __ init __ In tkinter?

__init__ method "__init__" is a reseved method in python classes. It is called as a constructor in object oriented terminology. This method is called when an object is created from a class and it allows the class to initialize the attributes of the class.

What is from tkinter import TTK?

from tkinter import * from tkinter.ttk import * That code causes several tkinter. ttk widgets ( Button , Checkbutton , Entry , Frame , Label , LabelFrame , Menubutton , PanedWindow , Radiobutton , Scale and Scrollbar ) to automatically replace the Tk widgets.


2 Answers

The root of the problem is that the Tkinter module is named Tkinter (capital "T") in python 2.x, and tkinter (lowercase "t") in python 3.x.

To make your code work in both Python 2 and 3 you can do something like this:

try:
    # for Python2
    from Tkinter import *
except ImportError:
    # for Python3
    from tkinter import *

However, PEP8 has this to say about wildcard imports:

Wildcard imports ( from <module> import * ) should be avoided

In spite of countless tutorials that ignore PEP8, the PEP8-compliant way to import would be something like this:

import tkinter as tk

When importing in this way, you need to prefix all tkinter commands with tk. (eg: root = tk.Tk(), etc). This will make your code easier to understand at the expense of a tiny bit more typing. Given that both tkinter and ttk are often used together and import classes with the same name, this is a Good Thing. As the Zen of python states: "explicit is better than implicit".

Note: The as tk part is optional, but lets you do a little less typing: tk.Button(...) vs tkinter.Button(...)

like image 92
Ben Gates Avatar answered Oct 02 '22 19:10

Ben Gates


The module is called tkinter, not Tkinter, in 3.x.

like image 21
Cat Plus Plus Avatar answered Oct 02 '22 20:10

Cat Plus Plus