gpt4 book ai didi

python - 我将如何制作一个每次在 tkinter 中显示帧时运行的方法

转载 作者:行者123 更新时间:2023-11-28 20:04:50 26 4
gpt4 key购买 nike

我有一个 gui 应用程序,它有几个窗口和按钮可以前进和后退。为此,我使用 Controller 并在每次窗口更改时将框架提升到顶部。这是我的 Controller 代码和一个典型的框架。

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 中编写的代码执行一次并创建 View ,但我想知道是否有可能有一个函数在每次引发帧时运行,类似于 Android 中的 onResume 函数。我想这样做,以防一些基础数据发生变化,就像这个上传页面中那样。例如,如果从填充 ListView 的数组中删除了一个项目,我希望能够刷新它。

最佳答案

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

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

首先,更改show_frame在窗口显示时将事件发送到窗口:

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

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

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...")

关于python - 我将如何制作一个每次在 tkinter 中显示帧时运行的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35029188/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com