如何在tkinter中创建一个方法,使其每次显示帧时都会运行

3

我有一个GUI应用程序,其中包含多个窗口和前进、后退按钮。为了实现这一功能,我使用了控制器,并在每次窗口更改时将框架提升到顶部。以下是我的控制器代码和典型框架。

import Tkinter as tk   # python
from tkFileDialog import askopenfilename, asksaveasfilename
from analyzer import Options

TITLE_FONT = ("Helvetica", 18, "bold")

class TennisProgram(tk.Tk):

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

        # the container is where we'll stack a bunch of frames
        # on top of each other, then the one we want visible
        # will be raised above the others
        container = tk.Frame(self)
        #Allow the window to be resized
        # container.resizable(width=True, height=True)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)
        #List of options bundles. One bundle for each video
        self.options_bundle_list = {}
        self.frames = {}
        #Init empty array to hold the list of files
        #Placed in the container so that all views can access it
        self.files = []

        for F in (UploadPage, AnalysisOptionsPage, FormAnalysisOptions, MatchAnalysisOptions, MatchAnalysisOptionsPage2, OutputPage, ProcessingPage):
            page_name = F.__name__
            frame = F(container, 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")
        # Name the window
        self.title("Tennis Analyzer")
        self.show_frame("UploadPage")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class UploadPage(tk.Frame):
#TODO Add logic to remove a file path from the list
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.createView()


    def createView(self):
        label = tk.Label(self, text="Upload Videos for Analysis", font=TITLE_FONT)
        label.pack(side="top", fill="x", pady=10)

        #Listbox to display the list of files to be processed
        self.path_list = tk.Listbox(self)
        #Additional options allow listbox to expand as the window is resized
        self.path_list.pack(fill=tk.BOTH ,expand=True)

        for path in self.controller.files:
            self.path_list.insert(tk.END, path)

        add_vidoes_button = tk.Button(self, text="Add Videos",
                            command=self.choose_files)
        continue_button = tk.Button(self, text="Continue",
                            command=lambda: self.controller.show_frame("AnalysisOptionsPage"))
        add_vidoes_button.pack(side=tk.TOP)
        continue_button.pack(side=tk.BOTTOM)

    def choose_files_paths(self):
        #don't want a full GUI, so keep the root window from appearing
        #self.controller.withdraw()
        #Get a file name from the user
        filename = askopenfilename()
        #Add it to the list of files to be processed
        self.controller.files.append(filename)
        self.path_list.insert(tk.END, filename)

我在init中编写的代码只会执行一次并创建视图,但我想知道是否有可能每次框架被激活时运行一个函数,类似于Android中的onResume函数。我希望这样做是为了在底层数据发生更改(例如在上传页面中)时进行刷新。举个例子,如果从填充列表视图的数组中删除了一个项目,我希望能够刷新它。


1
你可以在“show_frame”中添加代码。 - furas
@furas 显然我可以添加一些方法并拼凑出可用的东西,但我想知道是否存在某些公约或我忽略的文档。 - user3282276
2个回答

14

Tkinter有一些低级事件,比如<Visibility><Map>,它们应该在页面更改时触发。不幸的是,这些事件在所有平台上都不能可靠地工作。

最简单且最健壮的解决方案是生成自己的事件。您可以通过在<<>>中指定事件来创建并绑定自定义事件(例如:<<ShowFrame>>)。

首先,将show_frame修改为在显示窗口时向其发送事件:

def show_frame(self, page_name):
    ...
    frame.event_generate("<<ShowFrame>>")

接下来,如果需要在页面可见时得到通知,每个页面都可以绑定到此事件:

class UploadPage(tk.Frame):
    def __init__(self, parent, controller):
        ...
        self.bind("<<ShowFrame>>", self.on_show_frame)

    def on_show_frame(self, event):
        print("I am being shown...")

谢谢,这正是我在寻找的。 - user3282276
这看起来很不错。如果 on_show_frame 返回我需要分配的变量,那该怎么办? - Luke.py
1
@Luke.py:从绑定调用的函数无法返回数据,因为调用者不是您的代码。调用者是主循环,它忽略(几乎)所有返回的数据("几乎" 部分是指它会特殊处理返回字符串 "break")。 - Bryan Oakley
如果您愿意,我可以组合一个单独的问题。 - Luke.py
1
@martineau:是的。 - Bryan Oakley
显示剩余2条评论

0

时间过去了一点,但我找到了一个解决你的问题的方法(如果有人需要),它不需要一些集成函数,只需要时间库。

我制作了一个易于使用的函数。

def updatewindow(fps=60):
    time.sleep(1/fps)
    root.update()
    #root.update_idletasks() # usually you don't need this

这是一个使用此函数的示例片段

from tkinter import *
import time


root=Tk()

root.title("Clock")

timelbl=Label(root,font=('helvetica',40,'bold'),fg='red',bg='black')


def updatewindow(fps=60):
    time.sleep(1/fps)
    root.update()
    #root.update_idletasks() # usually you don't need this


timelbl.pack()

while True:
    timelbl["text"]=time.strftime("%H:%M:%S")
    updatewindow(fps=30)

你可以在 while 循环中运行任何东西,它将在每一帧都工作。 虽然不是最好的解决方案,但仍然是一个解决方案。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接