0

I am using PyCharm in Windows and I have downloaded Python 3.5.5 and all other libraries through Anaconda as well as tkinter module. Before this time I was using with it out any problems. Last time I installed live Streamer library. I think that cause problem. Tried Capital and Small:

from Tkinter import * , from tkinter import * , 
root = tk(), root = Tk(), root = tk.Tk(), root = tk.Tk(), root = Tk.tk()
root = TK.TK()

Here is the code I am running in PyCharm:

from tkinter import *
import tkinter

top = Tk()

def helloCallBack():
   print( "Hello Python", "Hello World")

B = tkinter.Button(top, text ="Hello", command = helloCallBack)

B.pack()
top.mainloop()

None of these work here

screenshot

  • the simple answer is you are trying to import `Tkinter` on your first line. This is going to throw an error because your version of python needs to import `tkinter` all lowercase. You cannot just throw every possible import option on there and hope for the best. If you import something that is not there it will error. Delete everything above the `import tkinter` line and change it to `import tkinter as tk` then use the `tk.` prefix for widgets. – Mike - SMT Aug 31 '18 at 11:41

3 Answers3

4

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:

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

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

  • Why? he already has `from tkinter import *`. why doesn't `Tk()` work as is? – blue_note Aug 31 '18 at 11:20
  • @blue_note explanation added. –  Aug 31 '18 at 11:20
  • Hmm, `import *` works fine for me in 3.6.4. Even the `tkinter` documentation uses it. – blue_note Aug 31 '18 at 11:24
  • True. I assumed that part of the answer from his problems. Did not have Python console to try at that time. Correcting it. However, it is true that it is recommended that way –  Aug 31 '18 at 11:27
  • `Tk()` does work as is... The OP just needs to delete all that other random invalid imports and tk instance attempts above the `import tkinter` line. – Mike - SMT Aug 31 '18 at 11:44
  • @Mike-SMT not true. Look at the picture in the editor. His exception is a `NameError`, not an `ImporError`. Look closely before donwvoting. –  Aug 31 '18 at 11:45
  • @J.C.Rocamonde the downvote was unintentional however the problem is actually the import. He is importing tkinter so `Tk()` wont work it needs to be `tkinter.Tk()`. The first line is commented out. If it was not then `Tk()` would work. – Mike - SMT Aug 31 '18 at 11:48
  • As I mentioned, the gray color in the first line is because PyCharm colours gray lines that have no effect in the code. Since Tk is not being recognised as part of that import, nowhere else the line is being used therefore it is highlighted as gray. It is not because it is commented out. @Mike-SMT –  Aug 31 '18 at 11:49
  • 1
    Well if both the first line doesnt work and `tkinter.Tk()` doesnt work then it is more likely that the OP doesnt have tkinter installed or its broken or removed for some reason. A fresh install of python should fix it. – Mike - SMT Aug 31 '18 at 11:52
  • Thanks Dear It worked after reinstalling Tk Module and Python – Hassan Afzal Dotani Sep 02 '18 at 19:06
  • Please, consider accepting the answer if it solved your problem and helped you. @HassanAfzalDotani –  Sep 02 '18 at 19:12
  • Mr. Rocamonde after Considering the Suggestions it was Salved – Hassan Afzal Dotani Sep 02 '18 at 19:17
  • @HassanAfzalDotani please read this https://stackoverflow.com/help/someone-answers –  Sep 02 '18 at 19:22
  • Sorry for that it was not in my Knowledge before. i want to vote for you but i can't reputation is less the 15 – Hassan Afzal Dotani Sep 02 '18 at 19:27
1

Remove the first 3 lines. You have tried all possible names. Most of them don't work. Either Tkinter or tkinter exists, and tk() does not.

blue_note
  • 27,712
  • 9
  • 72
  • 90
1

Lets clear up some basics as it appears you think several things should work that never will.

No mater how you import you will always need to do Tk() with an upper case T by itself or with the appropriate prefix.

Things you have tried that will never work.

root = tk(), Tk.tk(), root = TK.TK()

all lower case tk() or all uppercase TK() will never work in tkinter.

If from tkinter import * doesn't work and doing top = tkinter.Tk() doesn't work it is very likely that you do not have tkinter installed. Or at least it has been removed for some reason.

Windows distro should come with tkinter already. I would try to do a clean install and see what happens. You should update to 3.6 anyway as 3.5 has some bugs that needed to be fixed.

As for your import issue.

from tkinter import * This line should work fine with top = Tk(). So that tells me tkinter is not installed.

import tkinter This redundant line should work as top = tkinter.Tk() but if the previous is not working then this likely wont either.

After doing some testing on PyCharm I can say that if PyCharm failed to load tkinter then it would have errored out first on the import and not the Tk() portion.

Traceback (most recent call last):
  File "C:/Users/mcdomi3/PycharmProjects/MintyFlakes/test.py", line 1, in <module>
    from Tkinter import *
ModuleNotFoundError: No module named 'tkinter'

Process finished with exit code 1

With that little revaluation I think your install is corrupt.

Conclusion.

You need to reinstall python or try to pip install tkinter as it is missing from your libraries or corrupt somehow.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • What would be curious is if the first did not work but the latter would. Tk is a class in the `__init__.py` file; either the file was modified or if the import is successful then Tk should be in the namespace. –  Aug 31 '18 at 12:00
  • That would be interesting if `tkinter.Tk()` worked. By all rights though if the first does not work the latter should not. I think a simple update to 3.6 would fix the problem for the op thought. – Mike - SMT Aug 31 '18 at 12:02
  • 1
    @J.C.Rocamonde hum. I was testing some things in PyCharm as I have it but never used it before. If the library is not importable then the error should have been `ModuleNotFoundError: No module named 'tkinter'`. That is interesting. That tells me that pycharm did import tkinter and then failed to load `Tk()`... – Mike - SMT Aug 31 '18 at 12:06
  • Haha that is what I was trying to tell you. The error is indeed interesting. Should he provide more information, we may help him out better. But the error traceback and context is very reduced. –  Aug 31 '18 at 12:07
  • @J.C.Rocamonde ya I just woke up so my brain is still not fully functional lol. I did notice though that my PyCharm is not commenting out the first line when I write it in a way that wont work. – Mike - SMT Aug 31 '18 at 12:09
  • 1
    @J.C.Rocamonde I just noticed the OP is using Anaconda. This is likely the culprit. Anaconda contains hundreds of libraries and some of them do not work or do not work completely because each library is custom set up for Anaconda. I have seen many Q/A post on issues with Anaconda. – Mike - SMT Aug 31 '18 at 12:14
  • Thanks Dear It worked after reinstalling Tk Module and Python – Hassan Afzal Dotani Sep 02 '18 at 19:07