2017-07-20 35 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