Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中,如何根据按下的时间使按钮具有不同的输出?_Python - Fatal编程技术网

在Python中,如何根据按下的时间使按钮具有不同的输出?

在Python中,如何根据按下的时间使按钮具有不同的输出?,python,Python,我正在尝试学习如何编程一点。我正在为我的Raspberry Pi制作这个脚本,但是这个概念并不一定是专门针对它的。我想按下一个按钮,执行两个命令之一: 如果保持一秒钟,则运行命令A 如果保持5秒钟,则运行命令B 在等待B注册时,命令A可以运行多次 这是我的剧本,之后我会解释它的目的: import RPi.GPIO as GPIO import uinput inport time import os GPIO.setmode(GPIO.BCM) GPIO.setup(17, GPIO.IN)

我正在尝试学习如何编程一点。我正在为我的Raspberry Pi制作这个脚本,但是这个概念并不一定是专门针对它的。我想按下一个按钮,执行两个命令之一:

如果保持一秒钟,则运行命令A 如果保持5秒钟,则运行命令B 在等待B注册时,命令A可以运行多次

这是我的剧本,之后我会解释它的目的:

import RPi.GPIO as GPIO
import uinput
inport time
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.IN)

def main():
     events = (
          uinput.KEY_ESC,
          )

     device = uinput.Device(events)

     device.emit(uinput.KEY_ESC, 1) #Press
     device.emit(uinput.KEY_ESC, 0) #Release
下面是我想补充的两件事:

while 1:

     if time.sleep(1)
     if GPIO.input(17)==True:
          main()
          break

while 1:

     if time.sleep(10)
     if GPIO.input(17)==True:
          os.system("sudo reboot")
          break

本质上,这个脚本将使按钮有两个用途。按下它一秒钟将模拟ESC键的击键。按住它10秒钟将导致它重新启动系统。我怎么能让这两件事同时发生呢?学习Python对我来说是相当有挑战性的,但直到现在我还没有任何编程经验。

我不能针对您的特殊情况发言,但一般来说,当您需要按下按钮并测量时间时。也许有帮助

算法在注释中,我绘制了Python等价物:

import time
while 1:
    time.sleep(0.001) # do not use all the cpu power
    # make a loop to test for the button being pressed
    if button == pressed:
        when_pressed = time.time()
        while button == pressed:
            # wait until the button is not pressed any more
            time.sleep(0.001) # do not use all the cpu power
        # measure the time
        time_pressed = time.time() - when_pressed
        if time_pressed < too_short:
            continue # pressed too short do not use the other cases
        if 1 < time_pressed < 10:
            pass # pressed more than 1 second but less then 10
        if time_pressed > 10:
            pass # pressed more then 10 seconds 
            # a computer usually uses 6 seconds to wait for the shutdown
导入时间
而1:
时间。睡眠(0.001)#不要使用所有cpu电源
#做一个循环来测试被按下的按钮
如果按钮==按下:
按下时=time.time()
按下按钮==时:
#等待按钮不再按下
时间。睡眠(0.001)#不要使用所有cpu电源
#测量时间
按下时间=时间。时间()-按下时间
如果按下的时间<太短:
继续#按得太短请勿使用其他情况
如果1<时间\按下<10:
pass#按下超过1秒但不到10秒
如果按时间>10:
pass#按下超过10秒
#计算机通常用6秒钟等待关机

不过,我对覆盆子派没有经验,我决定回答你,因为看起来你已经失去了上师的关注。与本文相对应,我认为以下代码应该正常工作:

import os
import time
while True:
    if GPIO.input(17):
    #start counting pressed time
    pressed_time=time.monotonic()
    while GPIO.input(17): #call: is button still pressed
        # just to force process wait
        # may be possible use in this time.sleep(1) but I don't have confidence
        pass
    pressed_time=time.monotonic()-pressed_time
    if pressed_time<5:
        #send corresponding signal, you mentioned to call "main()", ok then:
        main()
    elif pressed_time>=5:
        os.system("sudo reboot")
导入操作系统
导入时间
尽管如此:
如果GPIO.input(17):
#开始计算按下的时间
按下时间=时间。单调()
GPIO.input(17)时:#呼叫:按钮仍被按下
#只是为了迫使进程等待
#这段时间可能有用。睡眠(1)但我没有信心
通过
按下的时间=时间。单调()-按下的时间
如果按下_time=5:
操作系统(“sudo重启”)

我对RPi没有概念,但每个UI工具包都有一个“后台”计时器和一个主等待循环。我将使用它而不是时间。睡眠巫婆阻止脚本。也许你不需要UI。您可以从raspberry Pi开始在后台运行此脚本。因此,您需要一个经常调用且不会阻塞的函数?这是另外一种情况,您的答案将是合适的。但我敢打赌,在某些UI之外,你会花很多时间来制作状态为“已按下/未按下”的按钮(这是wat OP提出的问题)。我刚刚看到了
GPIO
(硬件引脚),所以我猜问题是关于硬件按钮的。还有关机行为。。相当于我笔记本电脑上的一个硬件按钮。当然,您可以创建一个非常适合UI的答案。请随意使用此代码。