2017-07-20 14 views
0

我在樹莓pi3上製作了一個gui程序,我想從輸入(gpio)中獲取信號來執行一些命令,例如按下按鈕時生成一個信號。 我想從輸入中得到這個信號。 代碼是在gtk3,python,raspberry中創建輸入信號

import time 
import gi 
import RPi.GPIO as GPIO 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk 
GPIO.setmode(GPIO.BCM) 
GPIO.setwarnings(False) 
GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_DOWN) 

class gui: 
    inputValue = GPIO.input(18) 
    if inputValue == True: 
    self.label.set_text("There is input") 
    def on_window1_destroy(self, object, data=None): 
    print("quit with cancel") 
    Gtk.main_quit() 
    def on_okButton_clicked(self,button,data=None): 
    self.label.set_text("Waiting for input") 
    def __init__(self): 
    self.gladefile = "2.glade" 
    self.builder = Gtk.Builder() 
    self.builder.add_from_file(self.gladefile) 
    self.builder.connect_signals(self) 
    self.window = self.builder.get_object("window1") 
    self.label = self.builder.get_object("Label") 
    self.label.set_text("Hello") 
    self.window.show_all() 

if __name__ == "__main__": 
    main = gui() 
    Gtk.main() 

輸入僅在執行的開始被取和,當我使用一個循環的窗口凍結。 請幫助我。 謝謝。

+0

你有什麼代碼?你有沒有讀過像[這裏]的教程(http://raspi.tv/2013/how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-2)? – theGtknerd

+0

添加代碼。輸入只在執行開始時執行,當我使用循環時窗口凍結。 –

回答

0

我解決它通過爲波紋管編輯代碼:

import time 
import gi 
import RPi.GPIO as GPIO 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk 
GPIO.setmode(GPIO.BCM) 
GPIO.setwarnings(False) 
GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_DOWN) 

class gui: 

    def pin_callback (self, channel): 
    print ('pressed') 
    self.label.set_text("INput") 

    def on_window1_destroy(self, object, data=None): 
    print("quit with cancel") 
    Gtk.main_quit() 
    def on_okButton_clicked(self,button,data=None): 
    self.label.set_text("Waiting for input") 
    def __init__(self): 
    self.gladefile = "m.glade" 
    self.builder = Gtk.Builder() 
    self.builder.add_from_file(self.gladefile) 
    self.builder.connect_signals(self) 
    self.window = self.builder.get_object("window1") 
    self.label = self.builder.get_object("Label") 
    self.label.set_text("Hello") 
    self.window.show_all() 

if __name__ == "__main__": 
    main = gui() 
    GPIO.add_event_detect(18, GPIO.BOTH, callback=main.pin_callback) 
    Gtk.main() 

非常感謝你,theGtknerd。

1

我的PI被藏在一個盒子裏。這段代碼沒有經過測試,只是在我頭頂。告訴我它是否有效。

import time 
import gi 
import RPi.GPIO as GPIO 
gi.require_version('Gtk', '3.0') 
from gi.repository import Gtk 
GPIO.setmode(GPIO.BCM) 
GPIO.setwarnings(False) 
GPIO.setup(18, GPIO.IN,pull_up_down=GPIO.PUD_DOWN) 

class gui: 
    def pin_callback (self, channel): 
    print ('pressed') 
    def on_window1_destroy(self, object, data=None): 
    print("quit with cancel") 
    Gtk.main_quit() 
    def on_okButton_clicked(self,button,data=None): 
    self.label.set_text("Waiting for input") 
    def __init__(self): 
    self.gladefile = "2.glade" 
    self.builder = Gtk.Builder() 
    self.builder.add_from_file(self.gladefile) 
    self.builder.connect_signals(self) 
    self.window = self.builder.get_object("window1") 
    self.label = self.builder.get_object("Label") 
    self.label.set_text("Hello") 
    self.window.show_all() 
    GPIO.add_event_detect(18, GPIO.BOTH, callback=self.pin_callback) 

if __name__ == "__main__": 
    main = gui() 
    Gtk.main() 
+0

這是result.Traceback(最近呼叫的最後一個): 文件「/home/pi/Desktop/m.py」,第10行,在 class gui: File「/ home/pi/Desktop/m。 py「,第13行,在gui中 GPIO.add_event_detect(18,GPIO.BOTH,callback = self.pin_callback) NameError:name'self'未定義 –

+0

@MohamedGad編輯過的版本現在應該是另一種方法。 – theGtknerd

相關問題