Python 按下按键时退出轮询
Python break out of poll when key pressed
向新手致歉Python post,但谷歌搜索了一下,我仍然找不到我需要的东西。
我有以下 Pi Hat; https://github.com/modmypi/Jam-HAT, ...and I'm using their docs to guide me; https://gpiozero.readthedocs.io/en/stable/recipes.html
我的想法很简单;
- 运行 按下按钮时的脚本
- 等待 运行,显示闪光灯 运行宁
- 如果我按下另一个按钮,stop/kill 脚本
但是,我卡在了需要使用按钮停止脚本的部分
#!/usr/bin/env python3
import gpiozero
import subprocess
from signal import pause
from time import sleep
button1 = gpiozero.Button(18)
button2 = gpiozero.Button(19)
green1 = gpiozero.LED(16)
green2 = gpiozero.LED(17)
red1 = gpiozero.LED(5)
red2 = gpiozero.LED(6)
script = ""
yellow1 = gpiozero.LED(12)
yellow2 = gpiozero.LED(13)
def kill_hello():
global script
script.kill()
print("Killed PID: ", script.pid)
red1.on()
sleep(.5)
red1.off()
red2.on()
sleep(.5)
red2.off()
def say_hello():
global script
green1.on()
sleep(.5)
green1.off()
green2.on()
sleep(.5)
green2.off()
script = subprocess.Popen(['/home/kali/hello.sh'])
print("Script PID: ", script.pid)
while script.poll() is None:
if not button2.when_pressed:
green1.on()
sleep(.5)
green1.off()
sleep(1)
button2.when_pressed = kill_hello
print("Press Ctrl & C to Quit")
try:
button1.when_pressed = say_hello
pause()
except KeyboardInterrupt:
print( "\n...Quit!")
我已经尝试了 try
/except
,其中 while
循环所在,但也没有用(不像 KeyboardInterrupt
)。它无法识别已按下的按钮,我假设是因为它不在有效的 catch/else/something 块内。
有什么简单的跳出循环的建议吗?
似乎问题出在单个进程正在占用 Python,因此创建多个线程有帮助;
#!/usr/bin/env python3
import gpiozero
import subprocess
import time
import threading
from signal import pause
button1 = gpiozero.Button(18)
button2 = gpiozero.Button(19)
green1 = gpiozero.LED(16)
green2 = gpiozero.LED(17)
red1 = gpiozero.LED(5)
red2 = gpiozero.LED(6)
script = ""
yellow1 = gpiozero.LED(12)
yellow2 = gpiozero.LED(13)
switch = True
def blink():
def run():
while (switch == True):
green1.on()
time.sleep(.2)
green1.off()
time.sleep(.2)
if switch == False:
break
thread = threading.Thread(target=run)
thread.start()
def switchon():
global switch
global script
switch = True
print('switch on')
script = subprocess.Popen(['/home/kali/hello.sh'])
blink()
def switchoff():
global switch
global script
print('switch off')
script.kill()
switch = False
try:
button1.when_pressed = switchon
button2.when_pressed = switchoff
pause()
except KeyboardInterrupt:
print( "\n...Quit!")
向新手致歉Python post,但谷歌搜索了一下,我仍然找不到我需要的东西。
我有以下 Pi Hat; https://github.com/modmypi/Jam-HAT, ...and I'm using their docs to guide me; https://gpiozero.readthedocs.io/en/stable/recipes.html
我的想法很简单;
- 运行 按下按钮时的脚本
- 等待 运行,显示闪光灯 运行宁
- 如果我按下另一个按钮,stop/kill 脚本
但是,我卡在了需要使用按钮停止脚本的部分
#!/usr/bin/env python3
import gpiozero
import subprocess
from signal import pause
from time import sleep
button1 = gpiozero.Button(18)
button2 = gpiozero.Button(19)
green1 = gpiozero.LED(16)
green2 = gpiozero.LED(17)
red1 = gpiozero.LED(5)
red2 = gpiozero.LED(6)
script = ""
yellow1 = gpiozero.LED(12)
yellow2 = gpiozero.LED(13)
def kill_hello():
global script
script.kill()
print("Killed PID: ", script.pid)
red1.on()
sleep(.5)
red1.off()
red2.on()
sleep(.5)
red2.off()
def say_hello():
global script
green1.on()
sleep(.5)
green1.off()
green2.on()
sleep(.5)
green2.off()
script = subprocess.Popen(['/home/kali/hello.sh'])
print("Script PID: ", script.pid)
while script.poll() is None:
if not button2.when_pressed:
green1.on()
sleep(.5)
green1.off()
sleep(1)
button2.when_pressed = kill_hello
print("Press Ctrl & C to Quit")
try:
button1.when_pressed = say_hello
pause()
except KeyboardInterrupt:
print( "\n...Quit!")
我已经尝试了 try
/except
,其中 while
循环所在,但也没有用(不像 KeyboardInterrupt
)。它无法识别已按下的按钮,我假设是因为它不在有效的 catch/else/something 块内。
有什么简单的跳出循环的建议吗?
似乎问题出在单个进程正在占用 Python,因此创建多个线程有帮助;
#!/usr/bin/env python3
import gpiozero
import subprocess
import time
import threading
from signal import pause
button1 = gpiozero.Button(18)
button2 = gpiozero.Button(19)
green1 = gpiozero.LED(16)
green2 = gpiozero.LED(17)
red1 = gpiozero.LED(5)
red2 = gpiozero.LED(6)
script = ""
yellow1 = gpiozero.LED(12)
yellow2 = gpiozero.LED(13)
switch = True
def blink():
def run():
while (switch == True):
green1.on()
time.sleep(.2)
green1.off()
time.sleep(.2)
if switch == False:
break
thread = threading.Thread(target=run)
thread.start()
def switchon():
global switch
global script
switch = True
print('switch on')
script = subprocess.Popen(['/home/kali/hello.sh'])
blink()
def switchoff():
global switch
global script
print('switch off')
script.kill()
switch = False
try:
button1.when_pressed = switchon
button2.when_pressed = switchoff
pause()
except KeyboardInterrupt:
print( "\n...Quit!")