2015-12-05 238 views
-1

不明白爲什麼這個簡單的代碼不起作用。對不起,我的愚蠢。我有這樣的錯誤:在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() 
+0

bttnl,而不是bttn1寫道你必須在代碼'bttnl',而不是一個錯字'bttn1' – cuddlecheek

回答

1

你在你的代碼中的錯字。你在排隊14

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() 
+0

可能已經很容易了評論。 – Inkblot

+1

@Inkblot這是一個解決方案,爲什麼它應該在評論? – cuddlecheek

+0

因爲它沒有什麼太大的改變來保證答案,但它不是一個真正的大交易 – Inkblot

相關問題