python-3.x - 是否可以对 tkinter 进行多进程处理?

标签 python-3.x multithreading tkinter multiprocessing

我正在尝试对多个 tkinter 窗口进行多进程处理,其中每个进程都需要线程(对于使用 while 循环的长时间运行的进程)。这可能吗?

我试图通过纯多线程实现来解决这个问题——整个应用程序开始变得“​​迟钝”;因此现在尝试进行多处理。请参阅代码,了解我正在尝试做的非常简化的基本版本。有 2 个脚本:

主文件(运行)

from tkinter import *
from tkinter import ttk
import threading
import multiprocessing
import multiprocessing_import_worker

def hello():
    if __name__ == '__main__':
        p_list = ["Hello", "success"]
        p = multiprocessing.Process(target=multiprocessing_import_worker.worker, args=p_list)
        p.start()
        root.destroy()

root = Tk()
root.title("Window 1")

statusFrame = Frame(root)
statusFrame.pack()
statusLabelFrame = LabelFrame(statusFrame, text='TEST FRAME', font="Helvetica 8 bold")
statusLabelFrame.pack(fill="both", expand="yes", padx="7")
firstButtonFrame = Frame(root)
firstButtonFrame.pack()
statusbar = Label(statusLabelFrame, text="TEST", width=40, font="Helvetica 8 bold")
statusbar.pack()
b1=ttk.Button(firstButtonFrame, text="START SESSION", width=15, command=hello)
b1.pack(pady=(10,0))

root.mainloop()

辅助文件(作为单独的进程运行)

def worker(word, word2):
    import threading
    import tkinter as tk
    from tkinter import ttk
    import os

    print(word)
    print(word2)

    def testThread():
        global word
        global word2
        print(word)
        print(word2)

    root = tk.Tk()
    root.withdraw()
    rootWindow = tk.Toplevel()
    rootWindow.title("Window 2")

    statusFrame = tk.Frame(rootWindow)
    statusFrame.pack()
    statusLabelFrame = tk.LabelFrame(statusFrame, text='TEST FRAME', font="Helvetica 8 bold")
    statusLabelFrame.pack(fill="both", expand="yes", padx="7")
    firstButtonFrame = tk.Frame(rootWindow)
    firstButtonFrame.pack()
    statusbar = tk.Label(statusLabelFrame, text="TEST", width=40, font="Helvetica 8 bold")
    statusbar.pack()
    b1=ttk.Button(firstButtonFrame, text="START SESSION", width=15, state='disabled')
    b1.pack(pady=(10,0))

    child_thread1 = threading.Thread(target=testThread)
    child_thread1.start()

    rootWindow.protocol("WM_DELETE_WINDOW", os._exit(0))
    rootWindow.mainloop()

预期结果:

  • 将弹出标题为“Window 1”的 tkinter 窗口
  • 按下 START SESSION 后,“Window 1”被销毁(关闭)。
  • 然后弹出“窗口 2”(看起来完全一样),其 START SESSION 按钮被禁用。
  • 按右上角的叉号关闭“窗口 2”并结束应用程序。

实际结果:

  • 将弹出标题为“Window 1”的 tkinter 窗口
  • 按下 START SESSION 后,“Window 1”被销毁(关闭)。
  • 然后弹出另一个“窗口 1”(看起来完全一样),其启用的“开始 session ”按钮未映射到任何命令。
  • 按右上角的叉号关闭“窗口 1”并结束应用程序。

最佳答案

这是可能的,但是所有与 GUI 的交互必须在一个进程中进行。 Tkinter 本身不能跨进程,也不能跨线程。您的其他进程必须将工作放在 tkinter 进程定期轮询和执行的队列中(当然,您也可以与任何其他形式的 IPC 通信)。

这样做的原因是每个根窗口都与一个嵌入式tcl解释器相关联,而这个解释器本身不能跨进程。

关于python-3.x - 是否可以对 tkinter 进行多进程处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54484343/

相关文章:

java - 分布式任务队列中的并发(生产者/消费者)

python - 将直方图放入 tkinter 框架中

Python Tkinter 小部件参数混淆

python-3.x - 通过 Tkinter 中的列表变量修改列表框条目

c++ - Boost::Thread 函数导致嵌入式 ARM 上的段错误

python - SQLAlchemy再次过滤查询

python - Python 中反向集合运算符的实际使用

生成自定义类集合的 Pythonic 多重继承

c# - 事件不断引发,减慢代码速度。如何用线程处理它?

python - 如何使用数据透视表 Python 创建重复行