For restarting the script, you could try something like this:
Note: this method involves creating another script that when the button is clicked runs you original script.
Or You Could Try This:
Code:
from gpiozero import Buttonimport subprocessfrom signal import pauseimport sysdef restart_program(): try: subprocess.run(['python', 'test.py']) except KeyboardInterrupt: print("Exiting script...") sys.exit() finally: sys.exit()my_button = Button(17)my_button.when_pressed = restart_programpause()
Or You Could Try This:
Code:
from gpiozero import Buttonimport subprocessfrom signal import pauseimport sysdef restart_program(): try: # the code of your script. Make sure to indent everything correctly! Also be sure to put all your imports above. except KeyboardInterrupt: print("Exiting script...") sys.exit() finally: sys.exit()my_button = Button(17)my_button.when_pressed = restart_programpause()
Statistics: Posted by Henrik Gill — Sun Aug 18, 2024 11:57 pm