简体   繁体   English

使用 Tkinter 和 OOP 创建不同类的过渡帧(Python)

[英]Using Tkinter and OOP to create transition frames in different classes (Python)

I am creating frames for my project and so far I have it where it goes from the home page to the main page when the button is clicked.我正在为我的项目创建框架,到目前为止,当单击按钮时,我将它从主页转到主页。 The problem I am facing is when I try to go from the home page to the log page where I am faced with an issue of calling the show_frame() function (located in MainApplication) in class MainPage.我面临的问题是,当我尝试从主页到日志页面的 go 时,我遇到了在 ZA2F2ED4F8EBC06CBB4C21A2 中调用 show_frame() function(位于 MainApplication)的问题。

How would I go about using arguments in MainPage so I can move from main page to log page?我将如何 go 在 MainPage 中使用 arguments 以便我可以从主页移动到日志页面?

class MainApplication(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        # initialize frames
        self.frames = {}
        for F in (HomePage, MainPage, LogPage):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=0, column=0, sticky="nsew")

        # show home page frame first
        self.show_frame(HomePage)

    def show_frame(self, cont): # <-- FUNCTION HERE
        frame = self.frames[cont]
        frame.tkraise()

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

        continue_button = ttk.Button(self, text="Enter program", width=15,
                                     command=lambda: controller.show_frame(MainPage)) # <-- works here
        continue_button.pack()

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

    def success_actions(self):
        self.run_script_button["text"] = "View log"
        self.run_script_button.configure(
                     command=lambda: controller.show_frame(LogPage)) # <-- want to use here

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

        pass

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

def success_actions(self):
    self.run_script_button["text"] = "View log"
    self.run_script_button.configure(
                 command=lambda: self.controller.show_frame(LogPage))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM