Невозможно получить доступ к другому классу Tkinter - Python 3 - PullRequest
0 голосов
/ 28 августа 2018

Я пытаюсь вызвать другой класс в коде ниже, однако я получаю ошибку ниже:

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()

Я думаю, что проблема заключается в доступе init def для вызова контроллера?

Я просто не уверен, как ссылаться или передавать его через функцию init , чтобы затем "открыть" новую страницу.

Ценю всех и любую помощь.

1 Ответ

0 голосов
/ 28 августа 2018

Мне кажется, что вы хотите получить доступ к параметру контроллера, который вы передали функции __init__ вашего класса createMultiQuiz. Простой способ сделать это - создать атрибут контроллера в createMultiQuiz. Так что в __init__ напишите:

self.controller = controller

А потом смени

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

до

self.controller.show_frame(newQuestion)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...