简体   繁体   中英

tkinter not working on vs code. python version 3.9.6 (no module named tkinter.__main__;)

from tkinter import *
root = Tk()
myLabel = Label(root, text="Hello World")
myLabel.pack()
root.mainloop()

Every time I run this on VS Code it gives me this error NameError: name Tk is not define.

So I go into my console from VS code and manually put python tkinter.py (which is what I have the filename called and then it says no directory found. Still in the console on VS Code I change all directories so that I'm in the file which contains tkinter.py I run both these codes in console to see what's the issue (python tkinter.py and python3 tkinter.py and same error happens) Tk is not defined

lastly I tried to run this in my VS Code console python3 -m tkinter and it worked the GUI window popped up so I know I have it installed. it just isn't working when I try and run it in from the file (tkinter.py)

Try renaming your file. Sometimes you import the file you're coding in.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM