不明白爲什麼這個簡單的代碼不起作用。對不起,我的愚蠢。我有這樣的錯誤:在Python中使用Tkinter創建一個簡單的gui程序
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'
我的代碼是:
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.bttnl.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寫道你必須在代碼'bttnl',而不是一個錯字'bttn1' – cuddlecheek