Quantcast
Channel: Raspberry Pi Forums
Viewing all articles
Browse latest Browse all 3843

Python • Re: Button to restart Pi, button to restart program

$
0
0
For restarting the script, you could try something like 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()
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:      #  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



Viewing all articles
Browse latest Browse all 3843

Trending Articles