Python 在实例方法中调用实例变量时出现属性错误

Python 在实例方法中调用实例变量时出现属性错误,python,tkinter,Python,Tkinter,当从我的任何实例方法调用某些实例变量时,我会得到一个属性错误,表示所讨论的实例变量不存在。这个程序应该是一个用于测验的GUI,没有调用“问题标签”,工作时不会出错 我已经尝试了很多方法来绕过这个问题,包括将问题标签放在它自己的实例方法中,然后从radio_btn_gen调用所述方法,虽然有效,但这意味着我在结束屏幕上破坏它时遇到了麻烦,最终导致头痛。我浏览了堆栈溢出,试图找到与我的问题类似的东西,但大多数人的属性错误往往是拼写错误 import tkinter as tk LARGEFONT=(

当从我的任何实例方法调用某些实例变量时,我会得到一个属性错误,表示所讨论的实例变量不存在。这个程序应该是一个用于测验的GUI,没有调用“问题标签”,工作时不会出错

我已经尝试了很多方法来绕过这个问题,包括将问题标签放在它自己的实例方法中,然后从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)
发生异常:AttributeError “QuizApp”对象没有属性“questionlabel” radio\u btn\u gen中第29行的文件“C:\Users\jrich\Documents\stackoverflowquestion.py” self.questionlabel.grid(行=0,列=0)#在gui中创建问题标签 文件“C:\Users\jrich\Documents\stackoverflowquestion.py”,第16行,在\uuu init中__ self.radio_btn_gen() 文件“C:\Users\jrich\Documents\stackoverflowquestion.py”,第61行,在 app=QuizApp(根) 如果有人能在我的代码中发现任何其他问题,并愿意通知他们,我将不胜感激。如果有什么东西看起来很复杂或不合适,我仍然在学习,至少可以说,我的老师有一种“有趣”的编码方式。提前感谢您提供的任何帮助。

self.radio\u btn\u gen()
#以上是用于管理单选按钮列表的变量
self.questionlabel=tk.Label(父,text=self.qNa[self.page][0],font=SMALLFONT)

radio\u btn\u gen
中,您试图访问
self.questionlabel
,但在
中,您在设置
self.questionlabel

之前调用此方法。在
初始化
self.questionlabel
中,您正在调用
radio\u btn\u gen
,这让我觉得自己像个白痴。我认为我正确地阅读了错误消息。非常感谢。