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 *
Answer from alecxe on Stack OverflowIt'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 *
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
python - Import _tkinter or tkinter? - Stack Overflow
difference between from Tkinter import */import Tkinter as tk?
A little import confusion with tkinter
Difference between tk.Tk() and Tk()?
from Tkinter import * imports every exposed object in Tkinter into your current namespace.
import Tkinter imports the "namespace" Tkinter in your namespace and
import Tkinter as tk does the same, but "renames" it locally to 'tk' to save you typing
let's say we have a module foo, containing the classes A, B, and C.
Then import foo gives you access to foo.A, foo.B, and foo.C.
When you do import foo as x you have access to those too, but under the names x.A, x.B, and x.C.
from foo import * will import A, B, and C directly in your current namespace, so you can access them with A, B, and C.
There is also from foo import A, C wich will import A and C, but not B into your current namespace.
You can also do from foo import B as Bar, which will make B available under the name Bar (in your current namespace).
So generally: when you want only one object of a module, you do from module import object or from module import object as whatiwantittocall.
When you want some modules functionality, you do import module, or import module as shortname to save you typing.
from module import * is discouraged, as you may accidentally shadow ("override") names, and may lose track which objects belong to wich module.
You can certainly use
import Tkinter
However, if you do that, you'd have to prefix every single Tk class name you use with Tkinter..
This is rather inconvenient.
On the other hand, the following:
import Tkinter as tk
sidesteps the problem by only requiring you to type tk. instead of Tkinter..
As to:
from Tkinter import *
it is generally a bad idea, for reasons discussed in Should wildcard import be avoided?
_tkinter is a C-based module that wraps an internal tcl/tk interpreter. When you import it, and it only, you get access to this interpreter but you do not get access to any of the python classes.
You certainly can import _tkinter, but then you would have to recreate all of the python interfaces to the tcl/tk functions.
In python "_" marks a variable is intended for internal use
This convention is defined in PEP 8, but isn't enforced by Python
You shouldn't import class/modules/variables starting with "_" due to that nature, the developer should allow a property/setter methods to access those attributes..
For python2 use "Tkinter"
For python3 use "tkinter"
http://pep8.org/#descriptive-naming-styles
What is the difference between those two and what other ways are there to import Tkinter? And why are there many ways of importing?
When initializing the window, what is the difference between using tk.Tk() and Tk()? They both do the same thing but I don’t understand what’s happening under the hood.
I've been told it's not okay to import everything so how do I know what I need from Tkinter? Is there a list of what you can import from Tkinter that I can choose from?