【发布时间】:2010-03-30 10:10:26
【问题描述】:
我对 wxPython 比较陌生(但不是 Python 本身),所以如果我在这里遗漏了什么,请原谅我。
我正在编写一个 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() 和 these lines 将类放在同一个文件中来使其工作。如果有人能指出我正确的方向,那将不胜感激。
【问题讨论】:
标签: python user-interface events multithreading wxpython