Tkinter延迟后更改页面

时间:2019-09-29 21:47:26

标签: python 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方法。

1 个答案:

答案 0 :(得分:1)

您可以使用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-即。 LogoPageMainPageTemp类似于ButtonFrameButton等,它有助于识别代码中的类。有些工具为类使用特殊颜色-请在下面的代码中查看颜色。

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