Python Forum
tkinter control break a while loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
tkinter control break a while loop
#1
Hi
I have a basic simple Python 3.5 tkinter program on raspberry pi 3 that runs a stepper motor driven lift.
In the program I have a while loop that generates a square wave that drives the lift to end switches.
The program runs OK from end to end.
I wish to break out of the loop whenever certain button(s) is/are pressed.
My main problem is that the while loop disables the buttons.
If I can get the buttons enabled, I can accept a break command inside the loop.
(I am also unsure about the event.wait(time) that probably runs the same thread, but that's not a problem for now)

What would the right way to resolve and correct my program?
Thanks.
from tkinter import *
root = Tk()
import RPi.GPIO as GPIO
import threading
label = Label(root, text="Lift Control").pack()
GPIO.setwarnings(False)

PUL = 17  # Stepper Drive Pulses
DIR = 27  # Stepper Direction Bit.
ENA = 22  # Stepper Enable
UPSW = 18  # Down end-switch
DNSW = 23  # Up end switch

GPIO.setmode(GPIO.BCM)
GPIO.setup(PUL, GPIO.OUT)
GPIO.setup(DIR, GPIO.OUT)
GPIO.setup(ENA, GPIO.OUT)
GPIO.setup(DNSW, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
GPIO.setup(UPSW, GPIO.IN, pull_up_down=GPIO.PUD_UP)  
pause = 0.001  # For 500 Hz signal
event = threading.Event()

def up():
    dir = 1
    run(dir)
    return

def down():
    dir = 0
    run(dir)
    return

def hold():
    GPIO.output(ENA, GPIO.LOW)
    return

def run(dir):
    switch = UPSW
    if dir == 1:
        GPIO.output(DIR, GPIO.HIGH)
        switch = UPSW
    if dir == 0:
        GPIO.output(DIR, GPIO.LOW)
        switch = DNSW
    GPIO.output(ENA, GPIO.HIGH)
    event = threading.Event()
    
    while GPIO.input(switch) == 0:
        GPIO.output(PUL, GPIO.LOW)
        event.wait(pause)
        GPIO.output(PUL, GPIO.HIGH)
        event.wait(pause)
    GPIO.output(ENA, GPIO.LOW)
    print ('end run')
    return
    
buttonUp = Button(root,text = "to top",width = 15 ,command = up).pack()
buttonDown = Button(root,text = "to bottom",width = 15 ,command = down).pack()
buttonHold = Button(root,text = "Hold",width = 15 ,command = hold).pack()
                  
root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Code won't break While loop or go back to the input? MrKnd94 2 906 Oct-26-2022, 10:10 AM
Last Post: Larz60+
  How to break out of a for loop on button press? philipbergwerf 6 1,663 Oct-06-2022, 03:12 PM
Last Post: philipbergwerf
  break out of for loop? User3000 3 1,383 May-17-2022, 10:18 AM
Last Post: User3000
  Asyncio: Queue consumer gets out of while loop without break. Where exactly and how? saavedra29 2 2,602 Feb-07-2022, 07:24 PM
Last Post: saavedra29
  Cannot 'break' from a "for" loop in a right place tester_V 9 3,889 Feb-17-2021, 01:03 AM
Last Post: tester_V
  How to break a loop in this case? Blainexi 10 7,156 Sep-24-2020, 04:06 PM
Last Post: Blainexi
  how to break the loop? bntayfur 8 2,981 Jun-07-2020, 11:07 PM
Last Post: bntayfur
  break for loop Agusben 1 1,867 Apr-01-2020, 05:07 PM
Last Post: Larz60+
  Help For AutoGui Loop break ahmetnwpal 0 1,938 Mar-11-2020, 01:14 PM
Last Post: ahmetnwpal
  Do break operators turn while loop conditions from True to False? Drone4four 5 2,895 Oct-24-2019, 07:11 PM
Last Post: newbieAuggie2019

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020