2017-10-16 51 views
0

因此,我對編程非常陌生,我試圖解開我的第一個tkinter程序。它利用bioneer.com中的公式來計算一個人的「權力水平」(想想DBZ)。但是每當我運行它,我得到「AttributeError的:‘_tkinter.tkapp’對象有沒有屬性‘entry3’,我無法弄清楚什麼是錯如何修復tkinter程序中的AttributeError

import tkinter 

class powerapp_tk(tkinter.Tk): 
    def __init__(self,parent): 
     tkinter.Tk.__init__(self,parent) 
     self.parent = parent 
     self.initialize() 

    def initialize(self): 
     self.grid() 

     label = tkinter.Label(self, text="Enter Back Squat 1 Rep Max") 
     label.grid(column=0,row=0,columnspan=2,sticky='EW') 
     entry1 = tkinter.Entry(self) 
     entry1.grid(column=0,row=1,sticky='EW') 
     label2 = tkinter.Label(self, text="Enter Bench Press 1 Rep Max") 
     label2.grid(column=0,row=2,columnspan=2, sticky='EW') 
     entry2 = tkinter.Entry(self) 
     entry2.grid(column=0,row=3,sticky='EW') 
     label3 = tkinter.Label(self, text="Enter Max KM Covered in 12 min") 
     label3.grid(column=0,row=4,columnspan=2,sticky='EW') 
     entry3 = tkinter.Entry(self) 
     entry3.grid(column=0,row=5,sticky='EW') 
     label4 = tkinter.Label(self, text="Enter Max Pull Ups With Perfect Form") 
     label4.grid(column=0,row=6,columnspan=2,sticky='EW') 
     entry4 = tkinter.Entry(self) 
     entry4.grid(column=0,row=7,sticky='EW') 
     label5 = tkinter.Label(self, text="Enter Standing Vertical Jump in CM") 
     label5.grid(column=0,row=8,columnspan=2,sticky='EW') 
     entry5 = tkinter.Entry(self) 
     entry5.grid(column=0,row=9,sticky='EW') 
     label6 = tkinter.Label(self, text="Enter Max One Legged Tennis Ball Catch") 
     label6.grid(column=0,row=10,columnspan=2,sticky='EW') 
     entry6 = tkinter.Entry(self) 
     entry6.grid(column=0,row=11,sticky='EW') 
     label7 = tkinter.Label(self, text="Enter IQ Score") 
     label7.grid(column=0,row=12,columnspan=2,sticky='EW') 
     entry7 = tkinter.Entry(self) 
     entry7.grid(column=0,row=13,sticky='EW') 
     button = tkinter.Button(self, text=u"Calculate", command=self.calcpower) 
     button.grid(column=0,row=14) 

     self.grid_columnconfigure(0,weight=2) 
     self.minsize(width=400, height=400) 
     self.resizable(True, False) 
     self.update() 
     self.geometry(self.geometry()) 


    def calcpower(self): 
     h = entry3.get() * 100 
     i = entry4.get() * 5 
     j = entry5.get() * 15 
     k = entry6.get() * 8 
     l = entry7.get() * 2.5 
     powerlevel = int(a)+int(b)+int(h)+int(i)+int(j)+int(k)+int(l) 
     print('Your power level is: ' + str(powerlevel)) 

if __name__=="__main__": 
    app = powerapp_tk(None) 
    app.title('Power App') 
    app.mainloop() 

回答

1

我不得不移動一些東西,你的代碼還有其他一些問題,但這會讓你進一步。基本上,程序沒有意識到你的entry3被應用到不同的功能,其他選擇是宣佈它爲全球性但self.entry#是更好的解決方案

import tkinter 

class powerapp_tk(tkinter.Tk): 
    def __init__(self,parent): 
     tkinter.Tk.__init__(self,parent) 
     self.parent = parent 
     self.initialize() 

    def calcpower(self): 
     h = self.entry3.get() * 100 
     i = self.entry4.get() * 5 
     j = self.entry5.get() * 15 
     k = self.entry6.get() * 8 
     l = float(self.entry7.get()) * 2.5 
     powerlevel = int(h) + int(i) + int(j) + int(k) + int(l) 
     print('Your power level is: ' + str(powerlevel)) 

    def initialize(self): 

     self.grid() 

     label = tkinter.Label(self, text="Enter Back Squat 1 Rep Max") 
     label.grid(column=0,row=0,columnspan=2,sticky='EW') 
     entry1 = tkinter.Entry(self) 
     entry1.grid(column=0,row=1,sticky='EW') 
     label2 = tkinter.Label(self, text="Enter Bench Press 1 Rep Max") 
     label2.grid(column=0,row=2,columnspan=2, sticky='EW') 
     entry2 = tkinter.Entry(self) 
     entry2.grid(column=0,row=3,sticky='EW') 
     label3 = tkinter.Label(self, text="Enter Max KM Covered in 12 min") 
     label3.grid(column=0,row=4,columnspan=2,sticky='EW') 
     self.entry3 = tkinter.Entry(self) 
     self.entry3.grid(column=0,row=5,sticky='EW') 
     label4 = tkinter.Label(self, text="Enter Max Pull Ups With Perfect Form") 
     label4.grid(column=0,row=6,columnspan=2,sticky='EW') 
     self.entry4 = tkinter.Entry(self) 
     self.entry4.grid(column=0,row=7,sticky='EW') 
     label5 = tkinter.Label(self, text="Enter Standing Vertical Jump in CM") 
     label5.grid(column=0,row=8,columnspan=2,sticky='EW') 
     self.entry5 = tkinter.Entry(self) 
     self.entry5.grid(column=0,row=9,sticky='EW') 
     label6 = tkinter.Label(self, text="Enter Max One Legged Tennis Ball Catch") 
     label6.grid(column=0,row=10,columnspan=2,sticky='EW') 
     self.entry6 = tkinter.Entry(self) 
     self.entry6.grid(column=0,row=11,sticky='EW') 
     label7 = tkinter.Label(self, text="Enter IQ Score") 
     label7.grid(column=0,row=12,columnspan=2,sticky='EW') 
     self.entry7 = tkinter.Entry(self) 
     self.entry7.grid(column=0,row=13,sticky='EW') 
     button = tkinter.Button(self, text=u"Calculate", command=self.calcpower) 
     button.grid(column=0,row=14) 

     self.grid_columnconfigure(0,weight=2) 
     self.minsize(width=400, height=400) 
     self.resizable(True, False) 
     self.update() 
     self.geometry(self.geometry()) 


if __name__=="__main__": 
    app = powerapp_tk(None) 
    app.title('Power App') 
    app.mainloop() 
+0

謝謝!這幫助了一大堆,我想我可以處理你注意到的其他問題 – Drew

0

到處請自行添加前綴到所有輸入字段。而不是入門3,使用self.entry3

相關問題