Issue
I have made a gui program on raspberry pi3 and I want to get a signal from input (gpio) to execute some commands like when a button is pressed a signal is generated. I want to get this signal but from an input. the code is
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()
The input is taken only at the beginning of the execution and when I use a loop the window freezes. please help me. thanks.
Solution
I solved it by editing the code as bellow:
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()
Thank you very much, theGtknerd.
Answered By - Mohamed Gad