如何使用时间和没有按钮命令更改tkinter中的帧

时间:2017-09-05 06:50:07

标签: python python-3.x tkinter

我目前正在开发一个GUI,我希望我的GUI在最后一帧停留5秒,然后从最后一帧回到第一帧而不进行任何交互。

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

控制器

# 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课程

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课程

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课程

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

1 个答案:

答案 0 :(得分:3)

您可以通过多个地方拨打#fit, instead of transform and store this X_enc = LabelEncoder().fit(X_str.ravel()) enc_dct = {} for idx,items in enumerate(list(X_enc.classes_)): enc_dct[idx] = items print (enc_dct) #{0: 'a', 1: 'b', 2: 'cat', 3: 'dog', 4: 'green', 5: 'red'} 方法。 对我来说,如果将它放在控制器类中会更有意义,因为它是为了管理组件。

实现此目标的一种直接方法是完成after方法:

SeaofBTCapp.show_frame

调用def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() if cont == PageTwo: self.after(5000, self.show_frame, StartPage) 方法后,系统会安排以show_frame作为参数调用show_frame