2016-11-03 51 views
1

當前正在開發一個項目,該項目需要多線程與物理設備進行交互,同時打開活動框以發送指令。Python多重處理與框架

的問題是,當框架催生和「你好」按鈕被按下時,test方法運行,並且Process運行LEDBehavior.BlinkYellowLightEverySecond()正確是跑,但按鈕依然在它壓的圖形狀態,窗口變成反應遲鈍。

以下是此幀的代碼。

import LEDBehavior 

class MainFrame (Frame): 

    def __init__(self, master=None): 

     Frame.__init__(self, master) 
     self.pack() 
     self.createWidgets() 

    def createWidgets(self): 

     self.hi_there = Button(self) 
     self.hi_there["text"] = "Hello", 
     self.hi_there["command"] = self.test 

     self.hi_there.pack({"side": "left"}) 

    def test (self): 

     p1 = Process (LEDBehavior.BlinkYellowLightEverySecond()) 
     p1.start() 
     p1.join() 

# create the application 
myapp = MainFrame() 

# 
# here are method calls to the window manager class 
# 
myapp.master.title ("Sample Text inc, Security Software Professionals") 
myapp.master.maxsize (1920, 1080) 
myapp.master.minsize (800, 640) 

# start the program 
myapp.mainloop() 

回答

1

這是因爲:

p1.join() 

你告訴你的主進程等待P1。 BlinkYellowLightEverySecond看起來像可能會永久運行直到停止的函數類型,我猜你不想爲此停止你的程序。只需刪除join並添加一個停止/改變閃爍的按鈕。

編輯

二錯誤我錯過:

p1 = Process (LEDBehavior.BlinkYellowLightEverySecond()) 

眨眼之後的元組()......實際上使得通話,讓你調用你的主要工序中的功能!刪除()並正確調用構造函數:

p1 = Process (target = LEDBehavior.BlinkYellowLightEverySecond) 

請參閱https://docs.python.org/2/library/multiprocessing.html#multiprocessing.Process

+0

我已經刪除了行'p1.join()',但問題仍然存在,並且幀保持無響應狀態。 – Vanitas

+0

你可以發佈眨眼的代碼...? – kabanus

+0

http://pastebin.com/GePfrgzv – Vanitas