Where should I place GPIO.cleanup() - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Where should I place GPIO.cleanup() (/thread-25707.html) |
Where should I place GPIO.cleanup() - shallanq - Apr-09-2020 My code is really long with multiple programs running at the same time. Is it really a must to use GPIO.cleanup()? And if it is, where should I use it in my code? from tkinter import * import RPi.GPIO as GPIO from tkinter import ttk from tkinter import font as tkfont import time GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) GPIO.setup(17,GPIO.OUT)#motor GPIO.setup(27,GPIO.OUT)#LED GPIO.setup(26,GPIO.OUT)#buzzer GPIO.output (17, False) GPIO.output (27, False) GPIO.output (26, False) root = Tk() root.geometry("2000x1900") root.config(bg="light blue") root.frame = Frame(root) label = Label(root, bg = "light blue", fg = "dark blue", font=("Arial 50 italic")) label.place(relx=0.48, rely = 0.32, anchor = CENTER) title_label=Label(root, text="MEDICATION SCHEDULE", font = "Arial 80 ",bg="light grey",fg='black',relief='solid',bd=8) title_label.place(relx=0.47, rely = 0.16,anchor = CENTER) root.title("Smart Medicine Box") alarm1_input_compare= '' alarm2_input_compare= '' alarm3_input_compare = '' AFTER=None def set_alarm1(): global alarm1_input_compare alarm1_input_compare=comboHour1.get()+":"+comboMin1.get()+":00"+comboAM1.get() alarm1_input_display=comboHour1.get()+":"+comboMin1.get()+" "+comboAM1.get() alarm1_label.config(text="Time Set: "+alarm1_input_display) alarm1_label.place(relx=0.135, rely = 0.5, anchor = NW) print(alarm1_input_compare) comboHour1.place_forget() comboMin1.place_forget() comboAM1.place_forget() colon1_label.place_forget() morning_alarm_button.place_forget() change_morn_alarm_button.place(relx=0.2, rely = 0.58, anchor = CENTER) def change_alarm1(): change_morn_alarm_button.place_forget() morning_alarm_button.place(relx=0.198, rely = 0.58, anchor = CENTER) comboHour1.place(relx=0.16, rely = 0.52, anchor = CENTER) comboMin1.place(relx=0.2, rely = 0.52, anchor = CENTER) comboAM1.place(relx=0.235, rely = 0.52, anchor = CENTER) alarm1_label.place_forget() def set_alarm2(): global alarm2_input_compare alarm2_input_compare=comboHour2.get()+":"+comboMin2.get()+":00"+comboAM2.get() alarm2_input_display=comboHour2.get()+":"+comboMin2.get()+" "+comboAM2.get() alarm2_label.config(text="Time Set: "+alarm2_input_display) alarm2_label.place(relx=0.38, rely = 0.5, anchor = NW) print(alarm2_input_compare) comboHour2.place_forget() comboMin2.place_forget() comboAM2.place_forget() noon_alarm_button.place_forget() colon2_label.place_forget() change_aft_alarm_button.place(relx=0.45, rely = 0.58, anchor = CENTER) def change_alarm2(): change_aft_alarm_button.place_forget() noon_alarm_button.place(relx=0.45, rely = 0.58, anchor = CENTER) comboHour2.place(relx=0.415, rely = 0.52, anchor = CENTER) comboMin2.place(relx=0.46, rely = 0.52, anchor = CENTER) comboAM2.place(relx=0.495, rely = 0.52, anchor = CENTER) alarm2_label.place_forget() def set_alarm3(): global alarm3_input_compare alarm3_input_compare=comboHour3.get()+":"+comboMin3.get()+":00"+comboAM3.get() alarm3_input_display=comboHour3.get()+":"+comboMin3.get()+" "+comboAM3.get() alarm3_label.config(text="Time Set: "+alarm3_input_display) alarm3_label.place(relx=0.64, rely = 0.5, anchor = NW) print(alarm3_input_compare) comboHour3.place_forget() comboMin3.place_forget() comboAM3.place_forget() colon3_label.place_forget() change_even_alarm_button.place(relx=0.705, rely = 0.58, anchor = CENTER) def change_alarm3(): change_even_alarm_button.place_forget() even_alarm_button.place(relx=0.705, rely = 0.58, anchor = CENTER) comboHour3.place(relx=0.67, rely = 0.52, anchor = CENTER) comboMin3.place(relx=0.718, rely = 0.52, anchor = CENTER) comboAM3.place(relx=0.753, rely = 0.52, anchor = CENTER) alarm3_label.place_forget() def add_medication1(): noon_med_button["state"]='disabled' even_med_button["state"]='disabled' morn_med_Text.place(relx=0.21, rely = 0.85, anchor = CENTER) morn_med_label.place_forget() morn_med_button.place_forget() confirm_button1.place(relx=0.105, rely = 0.85, anchor = CENTER) keyboard(1) def confirm1(): noon_med_button["state"]='normal' even_med_button["state"]='normal' morn_med=morn_med_Text.get("1.0",END) morn_med_label.place(relx=0.21, rely = 0.88, anchor = CENTER) morn_med_Text.place_forget() confirm_button1.place_forget() morn_med_button.place(relx=0.21, rely = 0.72, anchor = CENTER) morn_med_label.config(text=morn_med) root.frame.grid_forget() def add_medication2(): morn_med_button["state"]='disabled' even_med_button["state"]='disabled' noon_med_Text.place(relx=0.45, rely = 0.845, anchor = CENTER) noon_med_button.place_forget() noon_med_label.place_forget() confirm_button2.place(relx=0.345, rely = 0.85, anchor = CENTER) keyboard(2) def confirm2(): morn_med_button["state"]='normal' even_med_button["state"]='normal' noon_med=noon_med_Text.get("1.0",END) noon_med_label.place(relx=0.46, rely = 0.88,anchor = CENTER) noon_med_Text.place_forget() confirm_button2.place_forget() noon_med_button.place(relx=0.458, rely = 0.72, anchor = CENTER) noon_med_label.config(text=noon_med) root.frame.grid_forget() def add_medication3(): noon_med_button["state"]='disabled' morn_med_button["state"]='disabled' even_med_Text.place(relx=0.72, rely = 0.85, anchor = CENTER) even_med_button.place_forget() even_med_label.place_forget() confirm_button3.place(relx=0.615, rely = 0.85, anchor = CENTER) keyboard(3) def confirm3(): noon_med_button["state"]='normal' morn_med_button["state"]='normal' even_med=even_med_Text.get("1.0",END) even_med_label.place(relx=0.72, rely = 0.88,anchor = CENTER) even_med_Text.place_forget() confirm_button3.place_forget() even_med_button.place(relx=0.72, rely = 0.72, anchor = CENTER) even_med_label.config(text=even_med) root.frame.grid_forget() def keyboard(number): root.frame = Frame(bg="light blue") root.frame.grid() def select(value): if number==1: cursor_position=morn_med_Text.index(INSERT) if value == "Clear": morn_med_Text.delete(1.0,END) elif value == "Enter": morn_med_Text.insert(INSERT,'\n') elif value=="Bkspce": text=morn_med_Text.get(1.0,cursor_position) newtext=text[:-1] morn_med_Text.delete(1.0,cursor_position) morn_med_Text.insert(INSERT,newtext) else: morn_med_Text.insert(cursor_position, value) elif number==2: cursor_position=noon_med_Text.index(INSERT) if value == "Clear": noon_med_Text.delete(1.0,END) elif value == "Enter": noon_med_Text.insert(INSERT,'\n') elif value=="Bkspce": text=noon_med_Text.get(1.0,cursor_position) newtext=text[:-1] noon_med_Text.delete(1.0,cursor_position) noon_med_Text.insert(INSERT,newtext) else: noon_med_Text.insert(cursor_position,value) else: cursor_position=even_med_Text.index(INSERT) if value == "Clear": even_med_Text.delete(1.0,END) elif value == "Enter": even_med_Text.insert(INSERT,'\n') elif value=="Bkspce": text=even_med_Text.get(1.0,cursor_position) newtext=text[:-1] even_med_Text.delete(1.0,cursor_position) even_med_Text.insert(INSERT,newtext) else: even_med_Text.insert(cursor_position, value) buttons=[ '~','1','2','3','4','5','6','7','8','9','0','Bkspce', 'q','w','e','r','t','y','u','i','o','p','-','"', 'a','s','d','f','g','h','j','k','l','+','Enter','Clear', 'z','x','c','v','b','n','m',',','.','/','(',')', ' ' ] label1=Label(root.frame,text='\n\n\n\n\n\n\n\n') label1.grid(row=7,columnspan=1) label2=Label(root.frame,text='\n\n\n\n\n\n\n') label2.grid(row=15,columnspan=1) varRow=9 varColumn=0 for button in buttons: command=lambda x=button: select(x) if button!=" ": Button(root.frame,text=button,width=7,bg="black",fg="white", activebackground="white",activeforeground="black",relief='raised',padx=0, pady=7,bd=4,command=command,font=("Arial",28 )).grid(row=varRow,column=varColumn) if button==" ": Button(root.frame,text=button,width=80,bg="black",fg="white", activebackground="white",activeforeground="black",relief='raised',padx=4, pady=7,bd=8,command=command,font=("Arial",15 )).grid(row=13,columnspan=17) varColumn+=1 if varColumn>11 and varRow==9: varColumn=0 varRow+=1 if varColumn>11 and varRow==10: varColumn=0 varRow+=1 if varColumn>11 and varRow==11: varColumn=0 varRow+=1 def alarm_message(): root.frame = Frame(bg="red") root.frame.grid() change_colour() title_message=Label(root.frame, text="TAKE YOUR \n MEDICATION ", font = "Arial 150 ",fg='white',bg='black') title_message.grid(row=0,column=0,padx=80,pady=300) taken_button = Button(root.frame, text="Done",command=button_pressed, font=("Arial", 80),bd=10) taken_button.grid(row=0,column=1,padx=45,pady=100) def change_colour(): global AFTER colour1=root.frame.cget("bg") if colour1=="red": GPIO.output(27, True) root.frame.config(bg="blue") if colour1=="blue": GPIO.output(27, False) root.frame.config(bg="red") AFTER=root.after(1000,change_colour) root.after(1000,change_colour) def button_pressed(): root.frame.grid_forget() print('Button pressed') root.after_cancel(AFTER) GPIO.output (27, False) GPIO.output (26, False) time.sleep(0.2) def tick(): current_time_display = time.strftime("%I:%M %p, %d %B %Y, %A", time.localtime()) current_time_compare = time.strftime("%I:%M:%S%p", time.localtime()) label.config(text=current_time_display) if (alarm1_input_compare == current_time_compare) or (alarm2_input_compare== current_time_compare) or (alarm3_input_compare== current_time_compare): root.frame.grid_forget() root.after_cancel(AFTER) print("1on") #GPIO.output (27, True)#LED GPIO.output (26, True)#buzzer GPIO.output (17, True)#motor time.sleep(5) GPIO.output (17, False) alarm_message() #updates label every 200ms label.after(200, tick) #morning combobox comboHour1= ttk.Combobox(root, width = 3, values=["01","02","03","04","05","06","07","08","09","10","11","12"],font=("Arial", 20)) comboMin1 = ttk.Combobox(root, width = 3,values=["00","01","02","03","04","05","06","07","08","09", "10","11","12","13","14" ,"15","16","17","18","19","20","21","22","23","24", "25", "26","27","28","29","30","31","32","33","34", "35","36","37", "40","41","42","43" ,"44","45","47","48", "50","51","54", "55","57"],font=("Arial", 20)) comboHour1.current(0) comboMin1.current(0) comboAM1 = ttk.Combobox(root, width = 3,values=["AM","PM"],font=("Arial", 20)) comboAM1.current(0) #set font for "Morning/Afternoon/Evening" labels fontlabel=tkfont.Font(family='Arial', size=46,weight=tkfont.BOLD,underline=1) #"Morning" label alarm1_label1=Label(root, text="Morning", font = fontlabel,bg="light blue") alarm1_label1.place(relx=0.135, rely = 0.4, anchor = NW) #time set label alarm1_label=Label(root,bg="light blue",font="Arial 20 italic bold",fg="maroon") #colon in between morning combobox colon1_label=Label(root, text=":", font = "Arial 20",bg="light blue") colon1_label.place(relx=0.176, rely = 0.504, anchor = NW) #button to set morning alarm morning_alarm_button = Button(root, text="Set Morning Alarm", command=set_alarm1,font=("Arial", 18)) change_morn_alarm_button = Button(root, text="Change Morning Alarm", command=change_alarm1,font=("Arial", 18)) change_alarm1() #afternoon combobox comboHour2= ttk.Combobox(root, width = 3, values=["01","02","03","04","05","06","07","08","09","10","11","12"],font=("Arial", 20)) comboMin2 = ttk.Combobox(root, width = 3,values=["00", "05", "10", "15", "20", "22","23","24","25", "30", "35", "40", "45", "50", "55"],font=("Arial", 20)) comboHour2.current(0) comboMin2.current(0) comboAM2 = ttk.Combobox(root, width = 3,values=["AM","PM"],font=("Arial", 20)) comboAM2.current(0) #"Afternoon" label alarm2_label1=Label(root, text="Afternoon", font = fontlabel,bg="light blue") alarm2_label1.place(relx=0.37, rely = 0.4, anchor = NW) #colon in between afternoon combobox colon2_label=Label(root, text=":", font = "Arial 20 ",bg="light blue") colon2_label.place(relx=0.435, rely = 0.504, anchor = NW) alarm2_label=Label(root, bg="light blue",font="Arial 20 bold italic",fg="maroon") #button to set afternoon alarm noon_alarm_button = Button(root, text="Set Afternoon Alarm", command=set_alarm2, font=("Arial", 18)) change_aft_alarm_button = Button(root, text="Change Afternoon Alarm", command=change_alarm2,font=("Arial", 18)) change_alarm2() #evening combobox comboHour3= ttk.Combobox(root, width = 3, values=["01","02","03","04","05","06","07","08","09","10","11","12"],font=("Arial", 20)) comboMin3 = ttk.Combobox(root, width = 3,values=["00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50", "55"],font=("Arial", 20)) comboHour3.current(0) comboMin3.current(0) comboAM3 = ttk.Combobox(root, width = 3,values=["AM","PM"],font=("Arial", 20)) comboAM3.current(0) #"Evening" label alarm3_label1=Label(root, text="Evening", font = fontlabel,bg="light blue") alarm3_label1.place(relx=0.64, rely = 0.4, anchor = NW) alarm3_label=Label(root, bg="light blue",font="Arial 20 bold italic",fg="maroon") #colon in between evening combobox colon3_label=Label(root, text=":", font = "Times 20",bg="light blue") colon3_label.place(relx=0.69, rely = 0.504, anchor = NW) #button to set evening alarm even_alarm_button = Button(root, text="Set Evening Alarm", command=set_alarm3, font=("Arial", 18)) change_even_alarm_button = Button(root, text="Change Evening Alarm", command=change_alarm3,font=("Arial", 18)) change_alarm3() #Textbox to input morning medications morn_med_Text=Text(root,width=18,height=8,font=("Arial", 20)) morn_med_label=Label(root,bg="light blue", font=("Arial",20)) morn_med_button = Button(root, text="Add Medication(s)", command=add_medication1, font=("Arial", 18),state='normal') morn_med_button.place(relx=0.21, rely = 0.72, anchor = CENTER) confirm_button1=Button(root, text="Confirm", command=confirm1, font=("Arial", 18)) med_label1=Label(root, text="Morning Medication(s): ",bg="light blue", font=("Arial 18 bold")) med_label1.place(relx=0.14, rely = 0.66) #Textbox to input afternoon medications noon_med_Text=Text(root,width=18,height=8,font=("Arial", 20)) noon_med_label=Label(root,bg="light blue", font=("Arial",20 )) #button to input medications;converts input in textbox to label noon_med_button = Button(root, text="Add Medication(s)", command=add_medication2, font=("Arial", 18),state='normal') noon_med_button.place(relx=0.458, rely = 0.72, anchor = CENTER) confirm_button2=Button(root, text="Confirm", command=confirm2, font=("Arial", 18)) med_label2=Label(root, text="Afternoon Medication(s): ",bg="light blue", font=("Arial 18 bold")) med_label2.place(relx=0.385, rely = 0.66) even_med_label=Label(root, bg="light blue", font=("Arial",20 )) #Textbox to input evening medications even_med_Text=Text(root,width=18,height=8,font=("Arial", 20)) #button to input medications;converts input in textbox to label even_med_button = Button(root, text="Add Medication(s)", command=add_medication3, font=("Arial", 18),state='normal') even_med_button.place(relx=0.72, rely = 0.72, anchor = CENTER) confirm_button3=Button(root, text="Confirm", command=confirm3, font=("Arial", 18)) med_label3=Label(root, text="Evening Medication(s): ",bg="light blue", font=("Arial 18 bold")) med_label3.place(relx=0.65, rely = 0.66) #call clock function tick() root.mainloop() RE: Where should I place GPIO.cleanup() - bowlofred - Apr-09-2020 You don't have to use it. If you set a pin as output and then connect it incorrectly (say set the pin to HIGH and then connect to ground), you can damage the device. GPIO.cleanup() just notices any pins you've set to output and returns them to input when called. If your program runs forever, you may not have any clean exit points. If you were really worried about this, you should probably set the pins to output only when actually controlling something, and then set them back to input when complete and waiting in between. But if it's in a case and you're not moving wires around, I wouldn't bother. RE: Where should I place GPIO.cleanup() - shallanq - Apr-11-2020 Alright, thank you! |