Can't import tkinter (or Tkinter) [duplicate]
Asked Answered
A

8

10

I am trying to import Tkinter to my project using Python 2.7 and instead I get the error:

ImportError: No module named tkinter

Before anyone says it, I have tried both "Tkinter" and "tkinter" but have gotten the exact same message.

Ayana answered 20/9, 2016 at 17:17 Comment(7)
Then Tkinter isn't available on your system. Are you using your system's built-in installation of Python, which may not include Tcl or Tkinter?Involucel
I believe so but I was sure I installed Tcl and TkinterAyana
Try to install the tk-python package and check this again. For windows #20045059 or for linux https://mcmap.net/q/75359/-install-tkinter-for-python-duplicateSpurgeon
read a little documentation, search this site. You'll find that the name of tkinter has changed between 2.x and 3.x.Cimbura
@BryanOakley he said he tried both tkinter and Tkinter. Do you mean something different?Commiserate
Which OS, python version, and installer?Egbert
Impressive how the "Before anyone says it, I have tried both "Tkinter" and "tkinter" but have gotten the exact same message" part was in the original version of the question, but almost everyone ignored that in order to propose changing the spelling, using wrapper code to try both spellings etc.Kala
T
11

First try using this code for your imports.

try:
    import Tkinter as tk # this is for python2
except:
    import tkinter as tk # this is for python3

If this doesn't work, try reinstalling tkinter. If you don't know how to reinstall tkinter look at the tkinter installation page, here.

Turk answered 2/10, 2016 at 22:59 Comment(0)
C
5

If you are using Ubuntu or Debian OS try this:-

sudo apt-get install python-tk

Or if you are using Python 3:-

sudo apt-get install python3-tk
Cinerarium answered 30/6, 2018 at 11:25 Comment(0)
S
2

Some compilers have tkinter preinstalled. For example, if you use IDLE tkinter is preinstalled. As much as I know, if you use IDLE, you have to click a box in order to install tkinter. If you are not using IDLE, check whether tkinter/Tkinter is included in your Site Packages folder. Consider reinstalling the compiler/interpreter you are using. After you made sure it is installed the syntax you have to use depends on the version of Python you are using. I am not quite sure for Python 2, but I think you write:

import Tkinter

For Python 3 you write:

import tkinter

or the more often used:

from tkinter import * 
Soelch answered 26/3, 2017 at 0:24 Comment(0)
H
2

if you install Python 3.7 you will need to type this:

from tkinter import *

then you can use tkinter.

Hydrophilic answered 5/4, 2019 at 13:8 Comment(0)
G
2

For Python 2 use this:

from Tkinter import *

For Python 3 use this:

from tkinter import *
Geronto answered 5/10, 2020 at 10:32 Comment(0)
S
0

Had the same problem ,please try this;

from Tkinter import *
Sacker answered 14/3, 2017 at 14:17 Comment(1)
Some times this doesn't work because you need to import it as somethingCultivation
D
0

Just try this, it's working on python 3.7

    import tkinter as tk
    from tkinter import simpledialog
    ROOT = tk.Tk()
    ROOT.withdraw()
    # the input dialog
    Uname= simpledialog.askstring(title="Hey Buddy",
                              prompt="Will you 
    please enter your good name")
Devisal answered 25/6, 2020 at 5:10 Comment(0)
L
-1

You should install the tkinter package for python. Tkinter has been renamed to tkinter in Python 3. The 2to3 tool will automatically adapt imports when converting your sources to Python 3.

Note: The following commands below are assuming that you run Debian or Ubuntu:

For Python 2:

sudo apt-get install python-tk

for Python 3:

sudo apt-get install python3-tk
Lamarlamarck answered 10/4, 2017 at 6:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.