0

I use MacOS Mojave and I just started learning tkinter in python 3.7.7, when I tried to run it (through sublime text) It shows this error:

    Traceback (most recent call last):
  File "/Users/julian/Documents/Ficheros_Python/tkinter.py", line 1, in <module>
    import tkinter as Tk
  File "/Users/julian/Documents/Ficheros_Python/tkinter.py", line 3, in <module>
    root = Tk()
TypeError: 'module' object is not callable
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "/Users/julian/Documents/Ficheros_Python/tkinter.py"]
[dir: /Users/julian/Documents/Ficheros_Python]
[path: /Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Library/Frameworks/Python.framework/Versions/3.7/bin:/Users/julian/opt/anaconda3/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/TeX/texbin:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Xamarin Workbooks.app/Contents/SharedSupport/path-bin]

I used to have anaconda but I uninstalled it a while ago and I have LaTeX installed but I don't now why its there or why there's xamarin, I need some help please.

Also this is the code I was working on (I also tried with from tkinter import * but the error its the same):

import tkinter as Tk

root = Tk()

myLabel = Label(root, text="Hello world")
myLabel2 = Label(root, text="Hello world2")

myLabel.grid(row=0, column=0)
myLabel2.grid(row=1, column=5)

root.mainloop()
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Jeffee102
  • 123
  • 2
  • 11
  • 2
    Change the name of your file to something other than `tkinter.py`. When you do `import tkinter` you're importing your own file. – Bryan Oakley Apr 13 '20 at 16:45
  • Does this answer your question? [Importing a library from (or near) a script with the same name raises "AttributeError: module has no attribute" or an ImportError or NameError](https://stackoverflow.com/questions/36250353/importing-a-library-from-or-near-a-script-with-the-same-name-raises-attribute) – Karl Knechtel Apr 29 '23 at 00:24

1 Answers1

0

In your statement import tkinter as Tk, you are renaming the module tkinter to Tk. To start tkinter you need the class Tk as shown on code above:

# here is your typo
from tkinter import Tk, Label


root = Tk()

myLabel = Label(root, text="Hello world")
myLabel2 = Label(root, text="Hello world2")

myLabel.grid(row=0, column=0)
myLabel2.grid(row=1, column=5)

root.mainloop()

You also need to rename your file from tkinter.py to anything that don't conflict into a module self-import.

  • In the OP's case, the problem is that they named their own file **tkinter.py**. No matter how they import it, it won't import the real tkinter module. – Bryan Oakley Apr 13 '20 at 16:46
  • Ok, I did that, the file now is test1.py, but now there's only one error, (the other part is the same): traceback (most recent call last): File "/Users/julian/Documents/Ficheros_Python/test1.py", line 2, in from tkinter import Tk, Label ImportError: No module named tkinter – Jeffee102 Apr 13 '20 at 17:03
  • Thanks for the update @BryanOakley, i have edited the answer. – lukasDamaceno Apr 13 '20 at 18:42
  • @JuliánBlanco there is a bunch of possible explanations for why your python executable is not finding the tkinter module. Could you please see to this question: https://stackoverflow.com/questions/25905540/importerror-no-module-named-tkinter? – lukasDamaceno Apr 13 '20 at 18:45
  • I already fixed it, I tried using pycharm and I found that it was using **Virtualenv Environment**, I just changed it to the system interpreter, thanks for the help. – Jeffee102 Apr 13 '20 at 19:12