所以我对编程非常陌生,而且我试图解开我的第一个tkinter程序。它利用thebioneer.com的公式来计算一个人的权力级别" (想想DBZ)。然而,每当我运行它时,我得到" AttributeError:' _tkinter.tkapp'对象没有属性' entry3'而且我无法弄清楚什么是错的。
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()
答案 0 :(得分: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()
答案 1 :(得分:0)
请在所有输入字段中添加自我前缀。而不是entry3,使用self.entry3。