1
我對wxPython比較新(但不是Python本身),所以請原諒我,如果我錯過了這裏的東西。wxPython,Threads和PostEvent之間的模塊
我正在編寫一個GUI應用程序,它在一個非常基本的層面上包括啓動和停止線程的「開始」和「停止」按鈕。這個線程是一個無限循環,只有當線程停止時纔會結束。該循環生成消息,目前僅使用print
輸出消息。
GUI類和無限循環(使用threading.Thread
作爲子類)保存在單獨的文件中。讓線程將更新推送到GUI中的TextCtrl
的最佳方式是什麼?我一直在玩PostEvent
和Queue
,但沒有多少運氣。
下面是一些裸露的骨頭代碼,以部分去除,以保持它的簡潔:
main_frame.py
import wx
from loop import Loop
class MainFrame(wx.Frame):
def __init__(self, parent, title):
# Initialise and show GUI
# Add two buttons, btnStart and btnStop
# Bind the two buttons to the following two methods
self.threads = []
def onStart(self):
x = Loop()
x.start()
self.threads.append(x)
def onStop(self):
for t in self.threads:
t.stop()
loop.py
class Loop(threading.Thread):
def __init__(self):
self._stop = threading.Event()
def run(self):
while not self._stop.isSet():
print datetime.date.today()
def stop(self):
self._stop.set()
我做到了,在有一點,讓它通過使用沿着的wx.lib.newevent.NewEvent()
在相同文件中的類來工作。如果任何人都能指出我正確的方向,那將非常感激。
+1,這是最簡單的方法。 – 2010-03-30 11:12:56
謝謝。我假設我的線程需要通過一個窗口(即在'MainFrame'中的'self'),以便我可以調用'wx.CallAfter(self.window.text_control.SetValue.SetValue,「some_text」)'在「循環」類中? – 2010-03-30 11:21:59
@sam是的,你需要訪問你想修改的控件。實現這一點的一種方式(與其他方式一樣)是將窗口傳遞給線程。 – 2010-03-30 14:24:43