在這里,我使用多處理在 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
有誰知道如何解決這個問題??提前致謝!
uj5u.com熱心網友回復:
這是一個如何與其他行程通信的示例,如果使用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()
有用:
- 行程間共享狀態
- 簡要介紹
tkinter和流程
另請參閱:
我強烈建議*在匯入某些內容時不要使用通配符(),您應該匯入您需要的內容,例如from module import Class1, func_1, var_2等等或匯入整個模塊:import module然后您也可以使用別名:import module as md或類似的東西,重點是除非您確實知道自己在做什么,否則不要匯入所有內容;名稱沖突是問題。
我強烈建議遵循PEP 8 - Python 代碼風格指南。函式名和變數名應該在snake_case,類名應該在CapitalCase. 沒有足夠的空間周圍=,如果它被用作關鍵字引數的一部分,( func(arg='value')),但周圍有空間,=如果是用于分配的值(variable = 'some value')。在運算子周圍留出空間( -/等value = x y:(此處除外value = x y))。在函式和類宣告周圍有兩個空行。物件方法定義周圍有一個空行。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350124.html
