如何使用时间而不使用按钮命令更改

如何使用时间而不使用按钮命令更改

本文介绍了如何使用时间而不使用按钮命令更改 tkinter 中的帧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发一个 GUI,我希望我的 GUI 在最后一帧停留 5 秒,然后从最后一帧返回到第一帧,无需任何交互.

I am currently working on a GUI where I want my GUI to stay 5 seconds on the last frame, and then come back from the last frame to the first frame without any interaction.

如果我使用 after 小部件方法,则计时器从应用程序的开头开始.如果我将它放在一个方法中,并且如果我调用该方法,则会发生相同的行为.另一方面,如果我通过按钮调用该函数,它会按预期工作.

If I use the after widget method, then the timer starts from the beginning of the application.If I put it inside a method, and if I call that method, the same behaviour ocurs. On the other hand, if I call that function through a button, it works as expected.

控制器

# The code for changing pages was derived from:
  http://stackoverflow.com/questions/7546050/switch-between-two-frames-in-tkinter
# License: http://creativecommons.org/licenses/by-sa/3.0/

import tkinter as tk


LARGE_FONT= ("Verdana", 12)


class SeaofBTCapp(tk.Tk):

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

        tk.Tk.__init__(self, *args, **kwargs)
        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 (StartPage, PageOne, PageTwo):

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

StartPage

StartPage class

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

        button = tk.Button(self, text="Visit Page 1",
                        command=lambda: controller.show_frame(PageOne))
        button.pack()

        button2 = tk.Button(self, text="Visit Page 2",
                        command=lambda: controller.show_frame(PageTwo))
        button2.pack()

PageOne

PageOne class

class PageOne(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page One!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                        command=lambda: controller.show_frame(StartPage))
        button1.pack()

        button2 = tk.Button(self, text="Page Two",
                        command=lambda: controller.show_frame(PageTwo))
        button2.pack()

PageTwo

PageTwo class

class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        label = tk.Label(self, text="Page Two!!!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = tk.Button(self, text="Back to Home",
                        command=lambda:self.start_reset())
        button1.pack()

        button2 = tk.Button(self, text="Page One",
                        command=lambda: controller.show_frame(PageOne))
        button2.pack()
        # self.after(5000,self.controller.show_frame(StartPage)) works only
        # once and the time starts from the start of the GUI and after 5000ms it takes
        # the gui to the start page no matter what
    def start_reset(self):
        self.after(5000,self.controller.show_frame(StartPage))


app = SeaofBTCapp()
app.mainloop()

推荐答案

您可以在多个地方调用 after 方法.对我来说,把它放在控制器类中更有意义,因为它是用来管理组件的.

There are several place where you can put your call to the after method.To me, it makes more sense if it's placed in the controller class, because it is meant to manage the components.

实现此目的的一种直接方法是完成SeaofBTCapp.show_frame 方法:

One direct way to achieve this is to complete the SeaofBTCapp.show_frame method:

def show_frame(self, cont):
    frame = self.frames[cont]
    frame.tkraise()

    if cont == PageTwo:
        self.after(5000, self.show_frame, StartPage)

show_frame 方法被调用时,会调度以StartPage 作为参数调用show_frame.

When the show_frame method is called, a call to show_frame with StartPage as parameter is scheduled.

这篇关于如何使用时间而不使用按钮命令更改 tkinter 中的帧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 17:19