Difference between tkinter and Tkinter

2019-01-17 16:06发布

问题:

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?

回答1:

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 *


回答2:

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


回答3:

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.



回答4:

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

#python 2
from Tinter import *

#python 3
from tkinter import *


回答5:

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.



回答6:

Use "import Tkinter" in Python 2 and use "import tkinter" in Python 3.



回答7:

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 *.



回答8:

try:
   import tkinter
   print"importing tkinter from python 3.x"
except:
   import Tkinter
   print"importing Tkinter from python 2.x"

finally:
   print"Difference !"