1

When I'm trying to run a script to see if I can use tkinter on VsCode it throws a NameError saying name 'Tk' is not defined. Furthermore I can run it on IDLE and it runs just fine. I've been searching around to see if I can fix it but I still can't get it working. Do you have any idea what I'm doing wrong?

Here is the code:

from tkinter import *

root = Tk()
myLabel = Label(root, text = 'Hello World!')
myLabel.pack()
martineau
  • 119,623
  • 25
  • 170
  • 301
lilted
  • 23
  • 5
  • 4
    Have you named the file `tkinter.py`? Because then it tries to import from itself. Also don't use `*` when importing, import either specific names or `import module` (`as md` ,can also use an alias), then prefix names from that module with `module.` (or `md.` if using an alias). – Matiiss Dec 30 '21 at 18:50
  • I have a file named tkinter.py, but I don't know if that's the correct tkinter file I got when I downloaded python. What should I write instead if I don't want it to import from itself? Why should don't use "*" ? Thank you for your help! – lilted Dec 30 '21 at 19:01
  • 2
    You need to rename your own `tkinter.py` to something else. – Bryan Oakley Dec 30 '21 at 19:02
  • 1
    There should NOT be a file called `tkinter.py`. `tkinter` is a directory. If you have a file called `tkinter.py`, that's what is screwing things up. – Tim Roberts Dec 30 '21 at 19:03
  • 1
    you can read about importing in this [answer](https://stackoverflow.com/a/2360808/14531062) – Matiiss Dec 30 '21 at 19:04

1 Answers1

2

Do NOT name your file tkinter.py because the tkinter module you are trying to import is actually importing the file itself. And since there's no function called Tk in your file, you are getting that error. Rename the file to something else.

For example, rename it to gui.py.

Also, it's better to be explicit rather than implicit in python. So instead of

# Pollutes your namespace 
# May clash with the functions you define or functions of other libraries that you import

from tkinter import * 

root = Tk()
...

you should use

import tkinter as tk

root = tk.Tk()
...

Here's an example of how it can clash with other namespaces:

from tkinter import *

root = Tk()
Label = "hello"
Label1 = Label(gui, text=Label)

This results in an error:

Traceback (most recent call last):
   File "stackoverflow.py", line 98, in <module>
     Label1 = Label(gui, text=Label)
 TypeError: 'str' object is not callable
krmogi
  • 2,588
  • 1
  • 10
  • 26
  • 2
    Your explicit/implicit comment is valid, but unfortunately, most of the `tkinter` tutorials date from 25 years ago, and still use the `from tkinter import *` thing. It's an uphill battle. – Tim Roberts Dec 30 '21 at 19:12
  • I changed the name of the file and now I could run it. Thank you for the help. Where can I learn about tkinter if most of the tutorials are old? – lilted Dec 30 '21 at 19:38
  • 1
    @lilted If doesn't really matter if tutorials use the wildcard import, when you follow along instead of using the function (i.e `Label()`), just do `tk.Label()`. There's also a pretty nice course I found using the `import tkinter as tk` import, https://www.youtube.com/watch?v=xuXYKhdoTsw – krmogi Dec 30 '21 at 19:49
  • 1
    @lilted This is the course I learned with (It's free!): https://edube.org/study/pcpp1-3 – krmogi Dec 30 '21 at 19:53
  • 1
    @lilted personally I use this reference (it still has some of the old names for a few modules but otherwise it's great): https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/, there is also an archive from effbot.org: http://web.archive.org/web/20201111171246/https://effbot.org/tkinterbook/ as well as little less comprehensive IMO but it has great examples and stuff this: https://www.tutorialspoint.com/python/python_gui_programming.htm – Matiiss Dec 30 '21 at 20:00