【问题标题】:run daemons only when frames are opened in tkinter仅当在 tkinter 中打开框架时才运行守护进程
【发布时间】:2020-11-05 19:52:39
【问题描述】:

我创建了 3 个框架,每个框架都有自己的守护进程,在框架之间导航我使用堆栈方法。一切正常,但我的问题是所有守护进程都在运行,即使我没有打开它们各自的框架,我知道我正在将帧加载到堆栈中,这就是所有守护进程开始运行的原因。 但是我希望那些守护进程只在它们的框架打开时运行,所以有什么办法可以做到。 示例 - Mainfile.py

import tkinter as tk                # python 3
from tkinter import font  as tkfont # python 3
#import Tkinter as tk     # python 2
#import tkFont as tkfont  # python 2
from Page1 import PageOne
from Page2 import PageTwo
class SampleApp(tk.Tk):

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

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")

        # 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)
        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):
            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.show_frame("StartPage")

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


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is the start page", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)

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

if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

上述文件是该文件中的一个主文件,我正在导入另外两个具有自己的守护进程的帧, 文件-Page1

import threading
import tkinter as tk
class PageOne(tk.Frame):
    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 1", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack() 
        thread1 = threading.Thread(target=self.func)
        thread1.daemon=True
        thread1.start()
      
    def func(self):
        print("this is thread1")

文件-Page2

import threading
import tkinter as tk
class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller
        label = tk.Label(self, text="This is page 2", font=controller.title_font)
        label.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Go to the start page",
                           command=lambda: controller.show_frame("StartPage"))
        button.pack()
        thread2 = threading.Thread(target=self.func)
        thread2.daemon=True
        thread2.start()
      
    def func(self):
        print("this is thread2")

在我运行整个程序后,我可以看到两个守护进程都给出了答案,但我希望它们仅在打开它们的框架后运行,例如当框架 PageOne 打开时,它的 thread-threa1 应该运行打印答案。

有没有我能做到的?

注意:这只是我给出的一个简单示例,但实际上我正在处理实时硬件值,因此通过使用线程和守护进程,我可以同时生成 UI 和硬件处理内容

【问题讨论】:

  • 您可以将线程代码的开始放在一个函数中,并在页面被提升时调用该函数。您还需要检查线程是否已经启动,否则会运行同一任务的多个线程。
  • 是的@acw1668,这是一个很好的建议,它奏效了,谢谢

标签: python tkinter threadpool python-multithreading python-daemon


【解决方案1】:

简单的解决方案是在需要框架之前不要创建框架。您可以通过我修改show_frame 来按需创建框架。

首先,您需要修改初始化 UI 的代码以创建框架。相反,它可以只创建从页面名称到页面类的映射。您需要删除以for F in (StartPage, PageOne, PageTwo): 开头的循环并将其替换为以下内容:

self.frames = {
    "StartPage": StartPage, 
    "PageOne": PageOne, 
    "PageTwo": PageTwo,
}

接下来,修改show_frame 以销毁任何现有框架,然后创建新框架。它应该看起来像这样:

def show_frame(self, page_name):
    # destroy the old frame
    for child in container.winfo_children():
        child.destroy()

    # create the new frame
    frame_class = self.frames[page_name]
    frame = frame_class(parent=self.container, controller=self)
    frame.pack(fill="both", expand=True)

另一种解决方案是保留原始代码,但仅在显示框架时启动线程。在此问题的已接受答案中可以看到如何在显示框架时运行函数的示例:How would I make a method which is run every time a frame is shown in tkinter

【讨论】:

  • 谢谢@Bryan Oakley 这是最好的事情
猜你喜欢
  • 2023-03-02
  • 2011-12-22
  • 2014-04-24
  • 2019-09-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-30
  • 1970-01-01
相关资源
最近更新 更多