Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/354.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法访问另一个类Tkinter-Python 3_Python_Python 3.x_Tkinter - Fatal编程技术网

无法访问另一个类Tkinter-Python 3

无法访问另一个类Tkinter-Python 3,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我试图在下面的代码中调用另一个类,但出现以下错误: self.__init__.controller.show_frame(newQuestion)() AttributeError: 'function' object has no attribute 'controller' 很抱歉代码混乱: class SeaofBTCApp(tk.Tk): def __init__(self, *args, **kwargs): tk.Tk.__init__ (self,

我试图在下面的代码中调用另一个类,但出现以下错误:

self.__init__.controller.show_frame(newQuestion)()
AttributeError: 'function' object has no attribute 'controller'
很抱歉代码混乱:

class SeaofBTCApp(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__ (self, *args, **kwargs)
        container = tk.Frame(self)

        tk.Tk.wm_title(self, "Quiz App")

        container.pack(side = "top", fill = "both", expand = True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}

        for F in(startPage, createMultiQuiz, newQuestion):

            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame(startPage)

    def show_frame(self, cont):

        frame = self.frames[cont]

        frame.tkraise()

class startPage(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="Hey brooo", font=LARGE_FONT)
        label.grid(row = 0, pady = 10, padx = 10)

        button1 = ttk.Button(self, text = "Create new quiz", command = lambda: controller.show_frame(createMultiQuiz))
        button1.grid(row = 1) 

class createMultiQuiz(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        label = tk.Label(self, text="Create a new multi choice quiz", font=LARGE_FONT)
        label.grid(row = 0)

        self.entryTitle = ttk.Entry(self)
        createLabel = ttk.Label(self, text= "Please enter the title for your multi choice quiz")
        createLabel.grid(row = 1, column = 0)
        self.entryTitle.grid(row = 1, column = 1)

        button1 = ttk.Button(self, text = "Submit", command = self.getInfo)
        button1.grid(row = 50, column = 0)
        button2 = ttk.Button(self, text = "Back to home", command = lambda: controller.show_frame(startPage))
        button2.grid(row = 50, column = 1)


    def getInfo(self):
        quizName = self.entryTitle.get()
        if not quizName:
            messagebox.showinfo("Quiz App", "You have not entered a Quiz Name!")
        else:
            choice = messagebox.askquestion("Quiz App", "You have named your Quiz : " + quizName + "\n Do you wish to continue", icon='warning')

        if choice == "yes":
            self.__init__.controller.show_frame(newQuestion)
        else:
            print("no")

class newQuestion(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)

        self.qType = tk.IntVar()

        label = tk.Label(self, text="Create a new Question", font=LARGE_FONT)
        label.grid(row = 0)

        createLabel = ttk.Label(self, text= "Please enter your question for question number #")
        self.entryTitle = ttk.Entry(self)
        createLabel.grid(row = 1, column = 0)
        self.entryTitle.grid(row = 1, column = 1)

        spaceLabel =tk.Label(self)
        spaceLabel.grid(row = 2)

        qLabel = tk.Label(self, text="What kind of question?")
        qLabel.grid(row = 3, column = 0) 
        multiQ = tk.Radiobutton(self, text="Multi choice question", padx = 20, variable=self.qType, value = 0)
        multiQ.grid(row = 4, column = 0, sticky = "w")
        openQ = tk.Radiobutton(self, text="A question that requires an answer", padx = 20, variable=self.qType, value = 1)
        openQ.grid(row = 5, column = 0, sticky = "w")

        button1 = ttk.Button(self, text = "Submit", command = self.getInfo)
        button1.grid(row = 50, column = 0)
        button2 = ttk.Button(self, text = "Back to home", command = lambda: controller.show_frame(startPage))
        button2.grid(row = 50, column = 1)

    def getInfo(self):
        qName = self.entryTitle.get()
        if not qName:
            print("nothing")
        else:
            print(qName)

        qType = self.qType.get()
        if qType == 0:
            print("qType = 0")
        if qType == 1:
            print("qType = 1")            

app = SeaofBTCApp()
app.mainloop()
我认为问题在于访问initdef调用控制器

我只是不确定如何引用或通过init函数“打开”一个新页面


感谢所有帮助。

在我看来,您似乎想访问传递给
CreateMultiquick
类的
\uuuu init\uuuu
函数的控制器参数。 一种简单的方法是在
createMultiquick
中创建一个控制器属性。因此,在
\uuuu init\uuuu
中,写下:

self.controller = controller
然后改变

self.__init__.controller.show_frame(newQuestion)()


欢迎来到SO。请尝试创建一个。您是一个传奇人物。谢谢你的帮助!
self.controller.show_frame(newQuestion)