Python 3.x 在Python中使用Tkinter创建简单的gui程序
我不明白为什么这个简单的代码不起作用。为我的愚蠢预先道歉。我有这样的错误:Python 3.x 在Python中使用Tkinter创建简单的gui程序,python-3.x,tkinter,Python 3.x,Tkinter,我不明白为什么这个简单的代码不起作用。为我的愚蠢预先道歉。我有这样的错误: Traceback (most recent call last): File "C:/python/coding/simple_gui", line 28, in <module> app=Application(root) File "C:/python coding/simple_gui", line 9, in __init__ self.create_widgets()
Traceback (most recent call last):
File "C:/python/coding/simple_gui", line 28, in <module>
app=Application(root)
File "C:/python coding/simple_gui", line 9, in __init__
self.create_widgets()
File "C:/python coding/simple_gui", line 14, in create_widgets
self.bttnl.grid()
AttributeError: 'Application' object has no attribute 'bttnl'
您的代码中有一个输入错误。您在第14行中编写了bttnl,而不是bttn1
from tkinter import *
class Application(Frame):
def __init__(self, master):
""" Initialize frame"""
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create 3 useless buttons"""
#first one
self.bttn1=Button(self, text ="I do nothing!")
self.bttn1.grid()
#second button
self.bttn2 = Button(self)
self.bttn2.grid()
self.bttn2.configure(text ="Me too!")
#third one
self.bttn3 = Button(self)
self.bttn3.grid()
self.bttn3["text"]="And me also!"
root=Tk()
#alter window
root.title("The simpliest gui")
root.geometry("200x100")
app=Application(root)
root.mainloop()
可能很容易成为一个注释。@墨迹是一个解决方案,为什么应该放在注释中?因为这不是一个很大的改变来保证答案,但它并不是一个大交易。你在代码
bttnl
中有一个拼写错误,而不是bttn1
from tkinter import *
class Application(Frame):
def __init__(self, master):
""" Initialize frame"""
super(Application, self).__init__(master)
self.grid()
self.create_widgets()
def create_widgets(self):
"""Create 3 useless buttons"""
#first one
self.bttn1=Button(self, text ="I do nothing!")
self.bttn1.grid()
#second button
self.bttn2 = Button(self)
self.bttn2.grid()
self.bttn2.configure(text ="Me too!")
#third one
self.bttn3 = Button(self)
self.bttn3.grid()
self.bttn3["text"]="And me also!"
root=Tk()
#alter window
root.title("The simpliest gui")
root.geometry("200x100")
app=Application(root)
root.mainloop()