首页 > 解决方案 > 使用 Tkinter 延迟后更改页面

问题描述

延迟后无法更改帧

如何在下面提供的代码中使用 logoPage 类中的 showFrames 方法?

class temp(tk.Tk):


def __init__(self, *args, **kwargs):
    tk.Tk.__init__(self, *args, **kwargs)
    tk.Tk.wm_title(self, "SOC_Workups")
    container_ = tk.Frame(self)

    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 (logoPage, mainPage):
        page_name = F.__name__
        frame = F(parent=container_, controller=self)
        self.frames[page_name] = frame

        # put all of the pages in the same location;
        # the one on the top of the stacking order
        # will be the one that is visible.
        frame.grid(row=0, column=0, sticky="nsew")

    self.showFrame("logoPage")



def showFrame(self, page):
    frame = self.frames[page]
    frame.tkraise()


class logoPage(tk.Frame):
def __init__(self, parent, controller):
    tk.Frame.__init__(self, parent)
    #label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
    #label.pack(pady = 10, padx = 10)
    canvas = tk.Canvas(self, height=500, width=1000)
    canvas.pack()

    logo = tk.PhotoImage(file='logo1.png')

    background_label = tk.Label(self, image=logo)
    background_label.image = logo
    background_label.place(relwidth=1, relheight=1)
    #parent.showFrame("mainPage")


class mainPage(tk.Frame):
def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        canvas = tk.Canvas(self, height=500, width=1000)
        canvas.pack()



app = temp()
app.mainloop()

该应用程序应以 logoPage 开头,然后在 10 秒后更改为 mainPage。我无法从 logoPage 类调用 showFrame 方法。

标签: pythontkinter

解决方案


您有权showPage()使用controller

controller.showFrame("mainPage")

要延迟运行,您可以使用root.after(millisecond, callback). 在您的代码中,它需要lambda创建使用参数运行函数的回调

parent.after(10000, lambda:controller.showFrame("mainPage"))

编辑:正如 Bryan Oakley 在评论中所说,你可以在没有的情况下运行它lambda

parent.after(10000, controller.showFrame, "mainPage")

中的第二个参数after()callback- 没有的函数名称()- 并且它的所有参数都作为下一个参数after()

我也在下面的代码中更改了它。


完整的工作代码:

PEP 8 - Style Guide for Python Code中有一个很好的规则,建议CamelCaseNames用于类 - 即。LogoPage, MainPage,Temp类似于Button, Frame,Button等。它有助于识别代码中的类。一些工具对类使用特殊颜色 - 请参阅下面代码中的颜色。

import tkinter as tk

class Temp(tk.Tk):

    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        tk.Tk.wm_title(self, "SOC_Workups")
        container_ = tk.Frame(self)

        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 (LogoPage, MainPage):
            page_name = F.__name__
            frame = F(parent=container_, controller=self)

            self.frames[page_name] = frame

            # put all of the pages in the same location;
            # the one on the top of the stacking order
            # will be the one that is visible.
            frame.grid(row=0, column=0, sticky="nsew")
        print(self.frames)
        self.showFrame("LogoPage")

    def showFrame(self, page):
        frame = self.frames[page]
        frame.tkraise()


class LogoPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        #label = tk.Label(self, text = "Start Page", font = LARGE_FONT)
        #label.pack(pady = 10, padx = 10)
        canvas = tk.Canvas(self, height=500, width=1000)
        canvas.pack()

        logo = tk.PhotoImage(file='logo1.png')

        background_label = tk.Label(self, image=logo)
        background_label.image = logo
        background_label.place(relwidth=1, relheight=1)
        #parent.showFrame("mainPage")
        #parent.after(10000, lambda:controller.showFrame("MainPage"))
        parent.after(10000, controller.showFrame, "MainPage")


class MainPage(tk.Frame):
    def __init__(self, parent, controller):
            tk.Frame.__init__(self, parent)
            canvas = tk.Canvas(self, height=500, width=1000)
            canvas.pack()

app = Temp()
app.mainloop()

推荐阅读