从我的任何实例方法中调用某些实例变量时,都会出现一个归属错误,表明所讨论的实例变量不存在。该程序应该是用于测验的GUI,并且无需调用“ questionlabel”即可正常工作。
我已经尝试了多种方法来绕过此问题,包括将questionlabel放入其自己的实例方法中,然后从radio_btn_gen调用该方法,该方法虽然有效,但是这意味着我无法在最终屏幕上销毁该方法,最终导致头痛。我浏览了堆栈溢出,试图找到与我的问题类似的东西,但大多数人往往会犯拼写错误的属性错误。
import tkinter as tk
LARGEFONT=("Impact",72)
SMALLFONT=("Verdana",12)
class QuizApp():
def __init__(self,parent):
self.qNa=(("question","answer","answer","answer"), ("question","answer","answer","answer"))#List of questions and answers
self.correctanswers=("answer")#All the correct answers
self.v = tk.StringVar()#stringVar for radiobuttons
self.page=0
self.radiobtnsframe=0
self.v.set("n/a")
self.rblist=[]
self.attemptlist=[]#stores all the attempts made on the quiz
self.radio_btn_gen()
#above are the variables used for managing the radio button list
self.questionlabel=tk.Label(parent,text=self.qNa[self.page][0],font=SMALLFONT)
self.rightlabel=tk.Label(font=SMALLFONT)#Displays correct answers at the end of quiz
self.confirmlabel = tk.Label(parent, textvariable = self.v) #tkinter converts IntVar to text for textvariable
self.confirmlabel.grid(row=1,column=1)
self.nxtbtn=tk.Button(text="Next",font=SMALLFONT,command=self.nxt_btn_cmd)#Changes the page to the next question when pressed
self.nxtbtn.grid(row=4,column=0)
def radio_btn_gen(self):
self.questionlabel.grid(row=0,column=0)#Creates the question label in the gui
self.radiobtnsframe=tk.Frame(relief="flat",borderwidth=2)# the frame that holds the radiobuttons
for i in self.qNa[self.page][1:]:#Iterates through inner lists of 'qNa' list and loads the radiobuttons into 'radiobtnsframe'
self.rb = tk.Radiobutton(master=self.radiobtnsframe,variable = self.v, value = i,
text= i,indicatoron=False,font=SMALLFONT)
self.rblist.append(self.rb)
self.rb.pack(fill="both")
self.radiobtnsframe.grid(row=1,column=0)
self.v.set("n/a")
def nxt_btn_cmd(self):#command called when next button pushed
rbvalue=self.v.get()#calls 'v' and assigns it to rbValue
if rbvalue != "n/a":#checks to see if any radiobuttons have been pushed
self.page+=1#increments page number
if self.page+1 > len(self.qNa):#checks to see if page number matches the number of questions
correctattempts=[i for i, j in zip(self.attemptlist, self.correctanswers) if i == j]#used to sort the correct answers into a list
self.rightlabel.config(text="you got {} questions right".format(len(correctattempts)))#displays how many answers you got right
self.rightlabel.grid(row=0,column=0)#No issue when called
self.radiobtnsframe.destroy()
self.confirmlabel.destroy()#similar label with no error attached when called
self.questionlabel.destroy()#attribute error when 'questionlabel' called
return #terminates method before executing the commands below
self.attemptlist.append(rbvalue)
self.radiobtnsframe.destroy()
self.radio_btn_gen()
del self.rblist[:]#clears the list storing the radiobutton data
if __name__ == "__main__":
root=tk.Tk()
app=QuizApp(root)#calls the app
root.mainloop
错误代码
Exception has occurred: AttributeError
'QuizApp' object has no attribute 'questionlabel'
File "C:\Users\jrich\Documents\stackoverflowquestion.py", line 29, in radio_btn_gen
self.questionlabel.grid(row=0,column=0)#Creates the question label in the gui
File "C:\Users\jrich\Documents\stackoverflowquestion.py", line 16, in __init__
self.radio_btn_gen()
File "C:\Users\jrich\Documents\stackoverflowquestion.py", line 61, in <module>
app=QuizApp(root)
如果任何人都可以在我的代码中发现任何其他问题,并希望将其告知,我将不胜感激。如果出现任何令人费解的问题或不合时宜的地方,我仍在学习,而且老师至少可以说是一种“有趣的”编码方式。预先感谢您提供的任何帮助。
答案 0 :(得分:0)
self.radio_btn_gen()
#above are the variables used for managing the radio button list
self.questionlabel=tk.Label(parent,text=self.qNa[self.page][0],font=SMALLFONT)
在radio_btn_gen
中,您尝试访问self.questionlabel
,但是在__init__
中,您在设置self.questionlabel
之前调用了此方法。