Reputation: 3
I have been coding in Python in Visual Studio Code. I have started with Tkinter. I was running the following code (see attached picture) and it did not work, however it did in the Python shell. Details are all in the picture, as to what the error is Image: [1]: https://i.sstatic.net/Nx0b6.png
This is the code:
from tkinter import *
root= Tk()
topFrame = Frame(root)
topFrame.pack()
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
button1 = Button(topFrame, text="button 1", fg="red")
button2 = Button(topFrame, text="button 2", fg="orange")
button3 = Button(topFrame, text="buton 3", fg="yellow")
button4 = Button(topFrame, text="button 4", fg="green")
button1.pack()
button2.pack()
button3.pack()
button4.pack()
root.mainloop()
error:
NameError: name 'Tk' is not defined
Upvotes: 0
Views: 138
Reputation: 1954
The python import priority:
Based on the provided screenshot, the script file have the same name as the module you are trying to use: tkinter
(see point N1 from the priority). Additionally, the import were done using "*", no verification is done on the import time, whatever module have Tk
object/class or not
To avoid the issue: rename your file to something else.
Best practices:
Upvotes: 2
Reputation: 405
If C:\Users...\Desktop\tkinter.py is your hand-made script file.
Try to rename C:\Users...\Desktop\tkinter.py to another. For example my_tkinter.py.
Probably Python use tkinter.py module file in same directory with visualStudioCode.py which are not default library module.
Upvotes: 0