这是我用来为YouTube视频下载程序创建GUI的代码
#import) tkinter as tk
# Create the main window
root = tk.Tk()
root.title("My GUI")
# Create label
label = tk.Label(root, text="Hello, World!")
# Lay out label
label.pack()
# Run forever!
root.mainloop()
我在我的终端中出现了如下错误
Traceback (most recent call last):
File "YoutubeVideoDownloader.py", line 4, in <module>
root = tk.Tk()
NameError: name 'tk' is not defined
Process finished with exit code 1.
请看我在这里分享的图片
您需要首先安装软件包,可以使用pip
来完成。使用某种cmd/terminal在项目目录文件夹上运行此操作。
pip install tk
只是取消评论进口
import tkinter as tk
上面的行告诉python将库tkinter导入为tk。然后可以使用tk.TK()
初始化tkinter库。