2014-05-02 104 views
0
import Tkinter,multiprocessing 

class cambiar(object): 
    def __init__(self,master,estado=False): 
     self.master = master 
     if estado == False: 
      self.run() 

    def run(self): 
     self.master['text'] = 'que tal' 

class body(object): 
    def __init__(self,win): 
     self.win = win 
     lista = ['label','botones'] 
     cont = len(lista) 
     for i in range(cont): 
      eval('self.'+str(lista[i]+'()')) 

    def label(self): 
     self.label_1 = Tkinter.Label(self.win,text='hola') 
     self.label_1.grid(row=0,column=0) 

    def botones(self): 
     self.boton_1 = Tkinter.Button(self.win,text='cambiar',command=lambda :self.win.proceso(self.label_1)) 
     self.boton_1.grid(row=1,column=0) 


class main(Tkinter.Tk): 
    def __init__(self): 
     Tkinter.Tk.__init__(self) 
     self.geometry('500x500') 
     self.b = body(self) 

    def proceso(self,wid): 
     self.p = multiprocessing.Process(target=cambiar,args=(wid,)) 
     self.p.start() 

root = main() 
root.mainloop() 

我有這樣的代碼,奇怪的是,我想要的是學習主進程可以改變屬性,比如在這種情況下的標籤,沒有人知道我該怎麼做?如何使用多重處理更改tkinter中的標籤?

回答

0
  1. 如果這是你的代碼,我不知道你爲什麼使用多處理而不是線程。
  2. 使用multiprocessing.Pool並等待異步呼叫的未來準備就緒。您可以通過安排一個測試未來狀態的功能root.after來等待。並行計算完成時更改標籤=未來有值時更改標籤。
+0

只使用多處理例如,實際的代碼是用於Kivy,但我使用tkinter進行測試,並且Kivy要求我爲程序使用多處理,有沒有辦法通過多處理來執行我想要的操作? – user3509050

+0

是的,有辦法,但他們不是很好,因爲你不能引用一個創建的對象。 http://stackoverflow.com/questions/21968278/multiprocessing-share-unserializable-objects-between-processes嘗試編號2.你不應該改變另一個進程的標籤,而是從主循環線程。 – User

+0

看看側欄。有很有用的鏈接。這是其中之一:http://stackoverflow.com/questions/15057789/multiprocessing-in-python-tkinter?rq=1 – User