我正在尝试编写一个使用wxPython GUI的Python程序。程序需要在后台收集一些信息(无限循环),但此时GUI应该是活动状态的。例如,如果我点击某个按钮,则某个变量或其他信息必须更改,并且在新周期中,此变量应代替旧变量使用。
但我不知道如何做到这一点。我认为必须使用线程,但我不了解如何使用它。
有人可以建议如何解决这个问题吗?
提前致谢!
但我不知道如何做到这一点。我认为必须使用线程,但我不了解如何使用它。
有人可以建议如何解决这个问题吗?
提前致谢!
from threading import Thread
class MyCollector(Thread):
def __init__(self, collect_from):
Thread.__init__(self) # must be called !
self.collect_from = collect_from
def run(self):
while True:
# .. collect ur things
collector_thread = MyCollector(my_source_to_collect_from)
collector_thread.start()
# go on with gui
示例2:
from threading import Thread
def collector(collect_from):
while True:
# .. collect ur things
collector_thread = Thread(target = collector, args = (my_source_to_collect_from,))
collector_thread.start()
# go on with gui