I write functional rather than class based code, because my applications are simple electromechanical control forms. I use a tkinter GUI to control a series of 4 step per rotation motors, and modulo to determine the next step. You fill in the number of cycles you want the motors to run and press go. They go CW, CCW for 'N' steps until a counter gets to the number of cycles. I monitor an abort button and use root.updateidletasks() to check to see if the abort button was pushed, preventing the motor run code from blocking.
Here is an example of the tkinter code:
while ((StepCounter < steps) and (stop == False)):
pin = StepCounter % 4
if(direction == True):
if pin == 0:
Step1()
time.sleep(StepTime)
elif pin == 1:
Step2()
time.sleep(StepTime)
elif pin == 2:
Step3()
time.sleep(StepTime)
elif pin == 3:
Step4()
time.sleep(StepTime)
if(direction == False):
if pin == 0:
Step3()
time.sleep(StepTime)
elif pin == 1:
Step2()
time.sleep(StepTime)
elif pin == 2:
Step1()
time.sleep(StepTime)
elif pin == 3:
Step4()
time.sleep(StepTime)
root.update_idletasks()
root.update()
if stop == True:
StopALL()
break;
StepCounter += 1
What is the equivalent to updateidletasks() in wx.python
Rolf - that's what I thought. I have a toggle button that flips a flag from false to true. here is the loop that blocks. I'm sure there is something really stupidly wrong in my logic that I'm not seeing.
def run(event):
count = 0
abort = False
rl = ctrl_txt_rl.GetValue()
print ('Abort button state = ', haltBtn.GetValue())
while count < int(rl) and (abort == False):
count = count
print ("Count = ", count)
#do some stuff
time.sleep(1)
count = count + 1
print ("Run Stopped")
return
OK Problem solved. I added a call to wx.Yield() into the while loop which refreshes the GUI and captures the button press. A pretty direct correlate to updateidletasks in tkinter which breaks the while loop. Seems easier than threading for me since my app is pretty straight forward.
count = 0
abort = False
rl = ctrl_txt_rl.GetValue()
print ('Abort button state = ', haltBtn.GetValue())
while count < int(rl) and haltBtn.GetValue() == False:
count = count
print ("Count = ", count)
print ('Abort button state = ', haltBtn.GetValue())
time.sleep(1)
wx.Yield()
count +=1
Thanks