I have a wxPython gui that is controlling some hardware. I need a button to disable while a function is running. The function also receives an argument value
let's say that I have this function that is bound to a button press:
def button_press(self, event):
in_val = self.num_in.GetValue() #grabs a value from a NumCtrl
TheThread(in_val) #initiates the thread with argument
btn = event.GetEventObject()
btn.Disable() #disables button
this function goes to the following thread class:
class TheThread(Thread):
def __init__(self, in_val):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.run(in_val)
def run(self, in_val):
print val
time.sleep(5)
wx.CallAfter(Publisher.sendMessage, "ctrl")
"""
threadsafe method to call a pub.subscribe that runs a
function to re-enable button
"""
This works improperly, as the gui freezes during the function run period, and the button does not properly disable.
How do I properly inherit this argument to allow this to run properly? Maybe something involving self.start() method?
You're right in your guess about the start
method.
run
is the method that gets invoked on the new thread, and start
is the method you want to call to tell the Thread
object to do that.
In your example, by calling run
yourself you're calling run
on the main thread and no threading is taking place at all. (the thread is never started)
class TheThread(Thread):
def __init__(self, in_val):
"""Init Worker Thread Class."""
Thread.__init__(self)
self.in_val = in_val
self.start()
def run(self):
print self.in_val
time.sleep(5)
wx.CallAfter(Publisher.sendMessage, "ctrl")
"""
threadsafe method to call a pub.subscribe that runs a
function to re-enable button
"""