Python名称错误:名称';框架';未定义(Tkinter)
代码如下:Python名称错误:名称';框架';未定义(Tkinter),python,tkinter,nameerror,Python,Tkinter,Nameerror,代码如下: #!/usr/bin/python from tkinter import * class App: def _init_(self, master): frame = Frame(master) frame.pack() self.lbl = Label(frame, text = "Hello World!\n") self.lbl.pack() self.button = Button(frame, tex
#!/usr/bin/python
from tkinter import *
class App:
def _init_(self, master):
frame = Frame(master)
frame.pack()
self.lbl = Label(frame, text = "Hello World!\n")
self.lbl.pack()
self.button = Button(frame, text="Quit", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Say hi!", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print("Hello!")
root = Tk()
root.title("Hai")
root.geometry("200x85")
app = App(root)
root.mainloop()
这里是错误:
Traceback (most recent call last):
File "F:/HTML/HTMLtests/python/hellotkinter2.py", line 4, in <module>
class App:
File "F:/HTML/HTMLtests/python/hellotkinter2.py", line 10, in App
self.lbl = Label(frame, text = "Hello World!\n")
NameError: name 'frame' is not defined
回溯(最近一次呼叫最后一次):
文件“F:/HTML/HTMLtests/python/hellotkinter2.py”,第4行,在
类应用程序:
文件“F:/HTML/HTMLtests/python/hellotkinter2.py”,第10行,在应用程序中
self.lbl=标签(frame,text=“Hello World!\n”)
NameError:未定义名称“frame”
找不到哪里出错了!感谢您的帮助 这里有点不对劲:
\uuuu init\uuuu
,而不是\uu init\uuu
中设置)和实例成员变量(在中设置)之间的区别。您使用的self
完全错误
您的错误是由2引起的,但在查看1和3之前,无法完全解决此问题。这里有一点错误:
\uuuu init\uuuu
,而不是\uu init\uuu
中设置)和实例成员变量(在中设置)之间的区别。您使用的self
完全错误
您的错误是由2引起的,但在查看1和3之前,无法完全解决此问题。缩进和大写与一些下划线一起关闭。下面的工作
#!/usr/bin/python
from Tkinter import *
class App(object):
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.lbl = Label(frame, text = "Hello World!\n")
self.lbl.pack()
self.button = Button(frame, text="Quit", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Say hi!", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print("Hello!")
root = Tk()
root.title("Hai")
root.geometry("200x85")
app = App(root)
root.mainloop()
缩进和大小写沿一些下划线关闭。下面的工作
#!/usr/bin/python
from Tkinter import *
class App(object):
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.lbl = Label(frame, text = "Hello World!\n")
self.lbl.pack()
self.button = Button(frame, text="Quit", fg="red", command=frame.quit)
self.button.pack(side=LEFT)
self.hi_there = Button(frame, text="Say hi!", command=self.say_hi)
self.hi_there.pack(side=LEFT)
def say_hi(self):
print("Hello!")
root = Tk()
root.title("Hai")
root.geometry("200x85")
app = App(root)
root.mainloop()