raspberry pi - python GPIO pins won't cleanup -
i beginer python , decided give go on raspberry pi. learned python small book covers basics, combined demo change tkinter window background color toggle button , lighting led code:
#! /usr/bin/env python import rpi.gpio gpio gpio.setmode(gpio.board); gpio.setup(7, gpio.out) gpio.output(7, false) tkinter import* window = tk() window.title("relay button") window.configure(bg= "green") btn_end= button(window, text = "close", command=exit) def tog(): if ( gpio.input(7) == true ): gpio.output(7, false) else: gpio.output(7, true) btn_tog=button( window, text="switch", command=tog) btn_end.pack(padx=100, pady=20) btn_tog.pack(padx=100, pady=20) window.mainloop() gpio.cleanup()
i error :
relaybutton.py:3: runtimewarning: channel in use, continuing anyway. use gpio.setwarnings(false) disable warnings. gpio.setmode(gpio.board); gpio.setup(7, gpio.out)
i have no clue why error, have @ end gpio.cleanup()
.
that's because program not executing part of code. when write tkinter application, mainloop()
infinite loop.
you can modify code make work.
#! /usr/bin/env python import rpi.gpio gpio tkinter import* def on_closing(): gpio.cleanup() window.destroy() gpio.setmode(gpio.board); gpio.setup(7, gpio.out) gpio.output(7, false) window = tk() window.title("relay button") window.configure(bg= "green") btn_end= button(window, text = "close", command=exit) def tog(): if ( gpio.input(7) == true ): gpio.output(7, false) else: gpio.output(7, true) btn_tog=button( window, text="switch", command=tog) btn_end.pack(padx=100, pady=20) btn_tog.pack(padx=100, pady=20) window.protocol("wm_delete_window", on_closing) window.mainloop()
you can see binded on_closing
function clean , destroy tk.tk window. hope helps.
Comments
Post a Comment