簡體   English   中英

如何解決 Tkinter 中的多處理問題?

[英]How to solve Problem with Multiprocessing in Tkinter?

在這里,我使用多處理在 tkinter 中運行多個算法。 起初我嘗試使用線程,但它不能在我的程序中正常工作。 下面是我的程序工作流程的一個想法,它的工作原理是這樣的,但只是不同的功能:

from tkinter import *
from multiprocessing import Process

def SquarFunc(Square):
    for i in range(1,1000):
        Square.set(str(i**2))

def CubeFunc(Cube):
    for i in range(1,1000):
        Cube.set(str(i**3))

if __name__ == "__main__":
    window= Tk()
    Square= StringVar()
    Cube= StringVar()
    window.geometry("500x500")
    A= Label(window, textvariable= Square)
    A.place(x=200, y=200)
    B= Label(window, textvariable= Cube)
    B.place(x=300, y=300)

    Squaring= Process(target=SquarFunc, args=(Square, ))
    Cubing= Process(target=CubeFunc, args=(Cube, ))
    Squaring.start()#Error originates here
    Cubing.start()
    Squaring.join()
    Cubing.join()
    window.mainloop()

產生的錯誤是這樣的:

TypeError: cannot pickle '_tkinter.tkapp' object

有誰知道如何解決這個問題?? 提前致謝!

這是一個如何與其他進程通信的示例,如果使用multiprocessing (解釋在注釋中, time.sleep僅用於示例,否則這些循環將在幾微秒內完成):

from tkinter import Tk, StringVar, Label
from multiprocessing import Process, Manager
import time


def square_func(d, name):
    for i in range(1, 1000):
        # update data in the shared dict
        d[name] = i
        time.sleep(0.1)


def cube_func(d, name):
    for i in range(1, 1000):
        # update data in the shared dict
        d[name] = i
        time.sleep(0.1)


def update_string_vars(d, *variables):
    for var in variables:
        # get the value from shared dict
        value = d[str(var)]
        if value is not None:
            # set string var to the value
            var.set(str(value))
    # schedule this to run again
    window.after(100, update_string_vars, d, *variables)


# cleanup process upon closing the window in case 
# processes haven't finished
def terminate_processes(*processes):
    for p in processes:
        p.terminate()


if __name__ == "__main__":
    window = Tk()
    window.geometry("500x500")
    # bind the terminator to closing the window
    window.bind('<Destroy>', lambda _: terminate_processes(
            square_process, cube_process))

    square_var = StringVar()
    cube_var = StringVar()

    Label(window, text='Square:').pack()
    Label(window, textvariable=square_var).pack()

    Label(window, text='Cube:').pack()
    Label(window, textvariable=cube_var).pack()
    
    # create the manager to have a shared memory space
    manager = Manager()
    # shared dict with preset values as to not raise a KeyError
    process_dict = manager.dict({str(square_var): None, str(cube_var): None})

    square_process = Process(
        target=square_func, args=(process_dict, str(square_var))
    )
    cube_process = Process(
        target=cube_func, args=(process_dict, str(cube_var))
    )
    square_process.start()
    cube_process.start()
    
    # start the updater
    update_string_vars(process_dict, square_var, cube_var)

    window.mainloop()

有用:

也可以看看:
我強烈建議在導入某些內容時不要使用通配符 ( * ),您應該導入您需要的內容,例如from module import Class1, func_1, var_2等,或者導入整個模塊: import module then 您也可以使用別名: import module as md或諸如此類,關鍵是不要導入所有內容,除非您確實知道自己在做什么; 名稱沖突是問題。

我強烈建議遵循PEP 8 - Python 代碼風格指南 函數名和變量名應該在snake_case ,類名應該在CapitalCase 如果=用作關鍵字參數的一部分( func(arg='value') ),則周圍沒有空格,但如果用於分配值( variable = 'some value' ),則=周圍沒有空格。 在運算符周圍留有空格( +-/等: value = x + y (此處value += x + y除外))。 在函數和類聲明周圍有兩個空行。 對象方法定義周圍有一個空行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM