如何等到你在python

时间:2017-02-01 03:56:32

标签: python raspberry-pi

当您按住按钮然后在释放该按钮时启动时,是否可以暂停python脚本? (我的按钮连接到我的Raspberry Pi上的GPIO引脚)

2 个答案:

答案 0 :(得分:1)

我假设您使用的按钮位于GPIO18中,因此您可以使用此代码。

 <DataGrid ItemsSource="{Binding Path= Shares}" HorizontalAlignment="Left" Margin="89,201,0,0" CanUserAddRows="False" AutoGenerateColumns="False" VerticalAlignment="Top" Height="280" Width="500">
       <DataGrid.Columns>
            <DataGridTextColumn Header="Company" Binding="{Binding CompanyName}" Width="250" />
            <DataGridTextColumn Header="Share Price" Binding="{Binding Price}" />                       
        </DataGrid.Columns>
    </DataGrid>

或者您也可以尝试:

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)

while True:
    input_state = GPIO.input(18)
    while not input_state:
        # as soon as your button is pressed you
        # will be inside this loop
        print('Button is being pressed')

我认为第二个更准确地针对您的要求。

答案 1 :(得分:0)

你看过gpiozero了吗?它使与GPIO的交互更加简单。

from gpiozero import Button

button = Button(2)

button.wait_for_press()    
button.wait_for_release()
print("Button was pressed and released")

以下是Button类的链接:https://gpiozero.readthedocs.io/en/v1.3.1/api_input.html#gpiozero.Button.wait_for_release

以及如何使用它的示例: https://gpiozero.readthedocs.io/en/v1.3.1/recipes.html#button