问题描述
限时送ChatGPT账号..import Tkinter
class Application(Frame):
def __init__(self, master):
Frame.__init__(self,master)
self.grid()
self.CreateWidgets()
def CreateWidgets(self):
self.LoginButton = Button(Self)
self.LoginButton["text"] = "Login"
self.LoginButton.grid()
self.QUIT_Button = Button(self)
self.QUIT_Button["text"] = "Quit"
self.QUIT_Button["command"] = self.quit
self.QUIT_Button["fg"] = "red"
root = Tk()
root.title("Login")
root.geometry("500x500")
app = Application(root)
root.mainloop()
这是我一直在关注的 youtube 教程:https://www.youtube/watch?v=YCLTv6wh3jE&index=39&list=PLB0701884E5AE1B45
This is the youtube tutorial that I have been following: https://www.youtube/watch?v=YCLTv6wh3jE&index=39&list=PLB0701884E5AE1B45
这是不断发生的错误:
Traceback (most recent call last):
File "C:\Users\omer\Desktop\test.py", line 3, in <module>
class Application(Frame):
NameError: name 'Frame' is not defined
我是 Python 的完全菜鸟,并且仍在学习中,因此我们将不胜感激.
I am a complete noob at Python and am still learning so any help would be appreciated.
推荐答案
Frame
Tk
和 Button
都位于Tkinter
命名空间.因此,您必须限定它们以让 Python 知道它们的位置1:
Frame
Tk
, and Button
are all located in the Tkinter
namespace. Thus, you have to qualify them to let Python know where they are1:
import Tkinter
class Application(Tkinter.Frame):
...
Tkinter.Frame.__init__(self, master)
...
self.LoginButton = Tkinter.Button(self)
...
self.QUIT_Button = Tkinter.Button(self)
...
root = Tkinter.Tk()
那个,或者你可以直接导入名称:
That, or you could just import the names directly:
from Tkinter import Frame, Tk, Button
<小时>
1如果您决定使用第一个解决方案,最好像这样导入 Tkinter
:
1If you decide to use this first solution, it would probably be best to import Tkinter
like this:
import Tkinter as tk
这样,代码就变成这样了:
That way, the code becomes this:
import Tkinter as tk
class Application(Tkinter.Frame):
...
tk.Frame.__init__(self, master)
...
self.LoginButton = tk.Button(self)
...
self.QUIT_Button = tk.Button(self)
...
root = tk.Tk()
这要简洁得多.
这篇关于Tkinter 问题 - 未定义名称框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论