Ok. First off:
What you shouldn't do:
In production, if you are not sure whether a module is called one way or another (which might be depending on the Python version that is installed), you should not put all the imports together like that, because if one fails it will raise an import error and that will crash your runtime. Do the following:
try:
import Tkinter
except ImportError: # Python 3.x present
import tkinter
However in your case you already know that you have Python 3 so that is not a problem. Just use the correct one (keep reading to the next section).
What it is recommendable that you do:
If you are using Python 2.x:
Module is named Tkinter
. You can do from Tkinter import *
and Tk
will be imported.
If you are using Python 3.x:
Module is named tkinter
. Note lowercase. You have to do import tkinter
; and use tkinter.Tk
Rationale
You might want to read this fragment from this answer already posted on SO:
However, PEP8 has this to say about wildcard imports:
Wildcard imports ( from 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(...)
Full answer: https://stackoverflow.com/a/11621141/4396006
Why your interpreter does not import Tk
I am uncertain on why your interpreter does not import Tk for that usage. You have to provide more details to be able to solve that part of your problem.
Edit: the line from tkinter import *
includes the namespace of the __init__.py
file in the tkinter
module folder into your file. Therefore you should check:
Where is PyCharm importing the tkinter module from. You can go to the tkinter
word in your import, get the contextual menu with the right click, and go to: Go to --> Declaration
(or just hit Ctrl+B
). It should take you to that __init__.py
file where Tk
should be a class defined in there.
Whether your Python Path when you run the file is fetching that folder you found the Tk module in.
If any of this is not ok, then it's probably because your installation is broken. I'd be helpful if you told us if only from tkinter import *
does not work or if import tkinter; tkinter.Tk
is not defined either. You should go for a clean install.
To help us know the root of the problem, try to run the same code from the terminal or in the Python's console and see what happens.