Reputation: 31
I'm trying to write a python program with wxPython GUI. Program must to collect some information in background (infinite loop), but GUI should be active at this time. Like, if I click on the some button, some variable or another information must change, and at the new cycle this variable should be used instead of the old.
But I don't know, how to make it. I think that I must use threading, but I don't understand how to use it.
Anyone can suggest how to solve this problem?
Thanks in advance!
Upvotes: 3
Views: 5183
Reputation: 67987
Have you considered having wxPython invoke an event handler of yours periodically, and perform the background processing in this? This depends on you being able to divide your work into discrete pieces, of course. Be aware that your background processing would have to be non-blocking so control would return to wxPython in a timely manner, to allow responsive GUI processing. Not sure what's the idiomatic way to implement such background processing in wxPython, but if I recall correctly the technique in (Py)Qt was to use a timer.
Upvotes: 0
Reputation: 33071
You definitely need to use threads to accomplish this. Then when you get some data from the non-GUI thread, you can use one of wxPython's thread-safe methods to let it know that it needs to update. Here's a little tutorial: http://www.blog.pythonlibrary.org/2010/05/22/wxpython-and-threads/
Or the perennial favorite: http://wiki.wxpython.org/LongRunningTasks
Another way to do it would be to create a socket server using Python's socket module and communicate with wx that way.
Upvotes: 1
Reputation: 16870
This is called "threading". Use pythons threading module.
Two examples:
example 1:
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
example 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
Upvotes: 0