Home > Net >  I am facing a 'name Tk is not defined' in VS code
I am facing a 'name Tk is not defined' in VS code

Time:05-04

I am using VS code for my python and I am getting an error saying

'Traceback (most recent call last):
  File "c:\Users\thecodeadd\OneDrive\Documents\Program\Python\Pyton programs\Fidget spinner.py", line 1, in <module>
    import tkinter
  File "c:\Users\thecodeadd\OneDrive\Documents\Program\Python\Pyton programs\tkinter.py", line 2, in <module>
    win= Tk()
NameError: name 'Tk' is not defined'

I have repaired my python file and searched on the internet but I have not been able to find any solutions for this.

CodePudding user response:

You didn't import the Tk class. Intead, you imported the tkinter module. To use the Tk class from the module you need to use win = tkinter.Tk().

It also looks like you named your file tkinter.py, which means that even if you do the above it won't work because your tkinter.py will get imported instead of the tkinter module. You should rename your file to something else besides tkinter.py.

  • Related