Python 3 Lesson 10: Break out of a loop

preview_player
Показать описание
How to break out of a loop in python using "break". This could be used to get back out of a menu.

Sources:

-Thin Lizzy Jailbreak. What a song.
Рекомендации по теме
Комментарии
Автор

THAT IS AWESOME! I didn't even know there was a break arg. I use it in C# all the time but I never knew there was a break arg which will take you out of a loop. OMG! So what if I want to go back and read the while True to make sure the button isn't pushed. Here's some code to give you a perspective.

from Tkinter import *
from threading import Thread
import RPi.GPIO as GPIO
import time
import pygame

GPIO.setmode(GPIO.BOARD)
GPIO.setup(36, GPIO.IN pull_up_down=GPIO.PUD_DOWN) #Input from Hopper Door Use
GPIO.setup(37, GPIO.IN pull_up_down=GPIO.PUD_DOWN) #Input from Fire Alarm
GPIO.setup(29, GPIO.OUTPUT) # Left Signal Bin selection
GPIO.setup(31, GPIO.OUTPUT) # Right signal Bin Selection
GPIO.setup(33, GPIO.OUTPUT) # Latch opener bypass relay
GPIO.setup(32, GPIO.OUTPUT) # Home Signal Bin Selection
POLLING_DELAY = 3000

Relay1 = (29, GPIO.OUTPUT)
Relay2 = (31, GPIO.OUTPUT)
Relay3 = (33, GPIO.OUTPUT)
Relay4 = (32, GPIO.OUTPUT)

#pygame sound effects
pygame.init()
click =
alarm =

class check_button(Thread):

# so in this instance I call for the labelText to have 3 different notifications.
# 1.) System is in Use as determined by input GPIO pin 36
# 2.) Fire Alert determined by input GPIO pin 37
# 3.) System Available if either inputs from GPIO are not True:

def __init__(status, labelText):
Thread.__init__(status)
status.labelText = labelText
status.b = False #set both GPIO inputs to false

def __init__(status2, label_Text):
Thread.__init__(status2)
status2.labelText = labelText
status2.b = False

def checkloop(status):
while True:
if GPIO.input(36) == 1: # IF GPIO detects input high pin 36 then System is in use!
if status.b == False :
self.labelText.set("System In USe")
print "System In Use"
status.b = True
else:
status.labelText.set("System Available") #If no input then System Available!
print "System Available"
status.b = False

def checkloop(status2):
while True:
if GPIO.input(37) == 1: # IF GPIO pin 37 input high then Fire Alert display!
if status2.c == False :
status2.buttonText.set("Fire Alert")
alarm.play() # 3 beep alarm
print "Fire Alert"
status2.b = True
else:
status.labelText.set("System Available") #If GPIO pin 37 is low System is Available!
print "System Available"
status2.b = False
while GPIO.input(37) == 1: pass

#Relay 1 is Paper meaning left Bin selection
#Relay 3 is Hopper Door latch bypass for that floor only!

Button Menu Paper Button

def selectOption1():
if Relay1.is_lit:
Relay1.off()
Relay3.off()
paperButton["text"] "Paper"
else:
if labelText["text"] != "System Available":
Relay1.on() #turn on relay to send 24V to main control Left Bin
Relay3.on() # turn on relay to control local latch
paperButton["text"] = "Please Wait"
labelText["text"]"System In Use!":
win.after(10000, delay_select_option1)

#Delay 10 seconds and go back to normal if Limit Switch is closed circuit

def delay_select_option1():
Relay1.off() # Signal Left Bin Selection
Relay3.off() # Open Latch
paperButton[“text”] = “Paper”
labelText[“text”] = “System Available”

Button

def selectOption2():
if Relay2.is_lit:
Relay2.off() #Right Bin Selection Signal Relay
Relay3.off() # Hopper door latch bypass is off and door is locked
plasticButton["text"] "Paper"
else:
if labelText["text"] != "System Available":
Relay2.on() #turn on relay to send 24V to main control Right Bin
Relay3.on() # turn on relay to control local latch
plasticButton["text"] = "Please Wait"
labelText["text"]"System In Use!":
win.after(10000, delay_select_option2) #Delay done now go to Finish function

#Delay 10 seconds and go back to normal if Limit Switch is closed circuit

def delay_select_option2():
Relay2.off() # Signal Right Bin Selection
Relay3.off() # Open Latch
plasticButton[“text”] = “Plastic”
labelText[“text”] = “System Available”

Glass/Metal Button

def selectOption3():
if Relay2.is_lit:
Relay2.off() #Right Bin Selection Signal Relay
Relay3.off() # Hopper door latch bypass is off and door is locked
glassButton["text"] "Glass/Metal"
else:
if labelText["text"] != "System Available":
Relay2.on() #turn on relay to send 24V to main control Right Bin
Relay3.on() # turn on relay to control local latch
glassButton["text"] = "Please Wait"
labelText["text"]"System In Use!":
win.after(10000, delay_select_option3) #Delay done now go to Finish function

#Delay 10 seconds and go back to normal if Limit Switch is closed circuit

def delay_select_option3():
Relay2.off() # Signal Right Bin Selection
Relay3.off() # Open Latch
glassButton[“text”] = “Glass/Metal”
labelText[“text”] = “System Available”


General Waste Button

def selectOption4():
if Relay4.is_lit:
Relay4.off() #Center Bin Selection Signal Relay
Relay3.off() #Hopper door latch bypass is off and door is locked
generalButton["text"] "General Waste"
else:
if labelText["text"] != "System Available":
Relay4.on() #turn on relay to send 24V to main control Right Bin
Relay3.on() # turn on relay to control local latch
generalButton["text"] = "Please Wait"
labelText["text"]"System In Use!":
win.after(10000, delay_select_option4) #Delay done now go to Finish function

#Delay 10 seconds and go back to normal if Limit Switch is closed circuit

def delay_select_option4():
Relay4.off() # Signal Right Bin Selection
Relay3.off() # Open Latch
generalButton[“text”] = “General Waste”
labelText[“text”] = “System Available”

End Button Function **** Kill All Processes and clean up upon

def exitProgram():
win.quit()
win.destroy()

Pack the GUI with

labelText1 = StringVar()
x1 = Label(win, textvariable=labelText1)
labelText=tk.Label(win, text='System Available', font=myFont, bg='orange', height=2, width=32) #Label to change according to limit switch condition
labelText.grid(row=0, sticky=tk.NSEW)

paperButton=tk.Button(win, text='Paper', font=myFont, command=selectOption1, bg='blue', height=2, width=38)
paperButton.grid(row=1, sticky=tk.NSEW)

plasticButton=tk.Button(win, text='Plastic', font=myFont, command=selectOption2, bg='red', height=2, width=38)
plasticButton.grid(row=2, sticky=tk.NSEW)

glassButton=tk.Button(win, text='Glass/Metal', font=myFont, command=selectOption3, bg='green', height=2, width=38)
glassButton.grid(row=3, sticky=tk.NSEW)

generalButton=tk.Button(win, text='General Waste', font=myFont, command=selectOption4, bg='purple', height=2, width=38)
generalButton.grid(row=4, sticky=tk.NSEW)

exitButton=tk.Button(win, text='Exit', font=myFont, command=exitProgram, bg='red', height=2, width=38)
exitButton.grid(row=5, sticky=tk.E)

win = Tk()
chk1 = check_button(labelText1)
c1 = # so here I tell it to go back through the loop
c1.start()

win.mainloop()

So if I use break, will it skip the loop while True arg to look to see if the fire or input from hopper door is in use?

yapandasoftware