34

When I answer Tkinter questions I usually try and run the code myself, but sometimes I get this error:

Traceback (most recent call last):
  File "C:\Python27\pygame2.py", line 1, in <module>
    from tkinter import *
ImportError: No module named tkinter

When I look at the question I see they import tkinter with a lower-case t:

from tkinter import *

I always import Tkinter with a capital T:

from Tkinter import *

Which always works for me. What is the difference between using tkinter and Tkinter?

fhdrsdg
  • 9,637
  • 2
  • 36
  • 58
Serial
  • 7,627
  • 13
  • 48
  • 67

12 Answers12

56

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 *
alecxe
  • 441,113
  • 110
  • 1,021
  • 1,148
  • 13
    In both cases, I recommend avoiding global imports. IMO a much better way is `import Tkinter as tk` or `import tkinter as tk`. – Bryan Oakley Jul 24 '13 at 21:50
  • 2
    Here is a link that lists all the `tkinter` differences: https://python-future.org/compatible_idioms.html#tkinter – slushy May 07 '19 at 12:09
  • It's 2021 and I can import both `tkinter` and `Tkinter` from python2.7 cli. This answer makes it seem like this shouldn't work. – Hendy Feb 25 '21 at 18:18
  • @Hendy That's kind of weird, sure you are on 2.7? – Delrius Euphoria Mar 06 '21 at 14:11
  • 1
    @CoolCloud so, after digging in I think this is actually about `future`, not `tkinter`! Check this: `$ dpkg -S /usr/lib/python2.7/dist-packages/tkinter/__init__.py` `python-future: /usr/lib/python2.7/dist-packages/tkinter/__init__.py`. So, I think that's why this was working for both variants. – Hendy Mar 07 '21 at 19:06
13

Tkinter is Python 2.x's name for the Tkinter library. In Python 3.x however, the name was changed to tkinter. To avoid running into this problem, I usually do this:

from sys import version_info
if version_info.major == 2:
    # We are using Python 2.x
    import Tkinter as tk
elif version_info.major == 3:
    # We are using Python 3.x
    import tkinter as tk
  • 10
    In both cases, I recommend avoiding global imports. IMO a much better way is `import Tkinter as tk` or `import tkinter as tk`. – Bryan Oakley Jul 24 '13 at 21:50
5

The capitalization of Tkinter and tkinter widget, method and option names is significantly different across the board. In some cases, the names themselves are different. Some features of Tkinter do not exist in tkinter, and vice-versa. But, as already stated, the main difference is that Tkinter is a module in Python 2x while tkinter is a module in Python 3x.

Onager
  • 89
  • 1
  • 6
3

It's simply that in Python 3 it's "tkinter" and in Python 2 it's "Tkinter" case in point:

#python 2
from Tkinter import *

#python 3
from tkinter import *

To make program run both on Python 2 and Python 3 you can use:

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

and then use Tkinter module as tk

Demian Wolf
  • 1,488
  • 2
  • 11
  • 28
rjdude31
  • 78
  • 1
  • 5
2

According to the official documentation, "Tkinter has been renamed to tkinter in Python 3". In Python2 you use import Tkinter or more often from Tkinter import * where "*" means "all". In Python3 you use import tkinter or from tkinter import *.

geizio
  • 97
  • 1
  • 15
1

Python 2 has always used from Tkinter import * but python 3 uses from tkinter import * I find this stupid and unfortunately it is confusing a lot of people.

Kenly
  • 19,646
  • 6
  • 41
  • 57
Jonah Fleming
  • 1,195
  • 3
  • 18
  • 31
1

Python2:

from Tkinter import *

Python3:

from tkinter import *
Nitrocell
  • 21
  • 2
1

In python 2, it is:

from Tkinter import *

In python 3, it is:

from tkinter import *

And if you want to make your code work in both versions, do:

try:
    from Tkinter import *
except:
    from tkinter import *
Andrew Shi
  • 93
  • 10
  • your answer is good (+1), but why do you need commenting under your answer too ? You'd better editing the answer with what you want to write. – Barbaros Özhan Jan 04 '20 at 10:45
0
try:
   import tkinter
   print"importing tkinter from python 3.x"
except:
   import Tkinter
   print"importing Tkinter from python 2.x"

finally:
   print"Difference !"
  • 4
    You should add a description of how your code helps solve the problem. – Tony Feb 26 '18 at 10:59
  • 2
    without the description it will be marked as low quality and can be deleted – Syfer Feb 26 '18 at 11:19
  • 2
    Not to mention that the result of this import is basically useless without further checks. – Mad Physicist Mar 06 '18 at 15:56
  • It's close to what I do. Instead alias the imports to the same name. Otherwise, little point: import Tkinter as tkinter. For the except: you could confine it just to import errors. – Robert Lugg Aug 24 '18 at 18:17
0

Additional Difference Between Tkinter and tkinter for Python 2. & Python 3.**

I had a script which had different imports of Python 2.* Tkinter so browsing a but I see that the answer are all scattered. Here a small summary with a safe Script for using both Python versions.

try:
    import Tkinter as tk
    import tkMessageBox as tkm
    import ScrolledText as tkst
    from tkFileDialog import askopenfilename
except ImportError:
    import tkinter as tk
    import tkinter.messagebox as tkm
    import tkinter.scrolledtext as tkst
    from tkinter.filedialog import askopenfilename

List of ModuleNotFoundError Errors (When Running Python 3.)

ModuleNotFoundError: No module named 'Tkinter'
ModuleNotFoundError: No module named 'tkMessageBox'
ModuleNotFoundError: No module named 'ScrolledText'
ModuleNotFoundError: No module named 'tkFileDialog'
Federico Baù
  • 3,772
  • 4
  • 18
  • 27
-1

The only difference is the module name that depends on the major version of Python. While in Python 3 it is tkinter, it used to be Tkinter in Python 2. Keep in mind, that other modules related to Tkinter were renamed as well. Here is the full list.

From the docs:

Tkinter has been renamed to tkinter in Python 3.

For backward compatibility, it's a good idea to import tkinter and the related modules you're using like this:

try:
    import tkinter as tk
    from tkinter import messagebox
    from tkinter import filedialog
except ImportError:
    import Tkinter as tk
    import tkMessageBox as messagebox
    import tkFileDialog as filedialog
Demian Wolf
  • 1,488
  • 2
  • 11
  • 28
-1

for python 2:

from Tkinter import *

For python 3:

from tkinter import * 

Else, try:

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