Reputation: 39
on a raspberry PI running raspbian i am using python + QT (PyQt4)
i am trying to display a value from an interrupt event when a gpio input change his state. Here is the python code :
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
import os
import random
import datetime
import sys
from threading import Thread
from PyQt4 import QtCore, QtGui, uic
qtCreatorFile = "form.ui" # Enter file here.
global Ui_MainWindow, QtBaseClass
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
global Start
Tmp = datetime.datetime(2000,12,14)
Start = Tmp.today()
print str(Start)
def my_Start(channel): #Interrupt 18
Tmp = datetime.datetime(2000,12,14)
Start = Tmp.today()
def my_Stop(channel): #Interrupt 24
Tmp = datetime.datetime(2000,12,14)
Stop = Tmp.today()
print str(Stop-Start)
self.label.setText(str(Stop-Start))
GPIO.add_event_detect(18, GPIO.FALLING, callback=my_Start,bouncetime=2000)
GPIO.add_event_detect(24, GPIO.RISING, callback=my_Stop,bouncetime=2000)
class MyApp(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
Ui_MainWindow.__init__(self)
self.setupUi(self)
self.lcdNumber.display(10.1)
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
The problem and that self isn't recognize in my_stop callback. So my question : How can i update my GUI from this callback.
Thanks
With the answer below it works The only things to modify are :
define them like that :
def my_Start(self,channel):
def my_Stop(self,channel):
Declare the event like that :
if __name__ == "__main__":
app = QtGui.QApplication(sys.argv)
window = MyApp()
GPIO.add_event_detect(18, GPIO.FALLING, callback=window.my_Start,bouncetime=2000)
GPIO.add_event_detect(24, GPIO.RISING, callback=window.my_Stop,bouncetime=2000)
window.show()
sys.exit(app.exec_())
Thanks to helpers
Upvotes: 0
Views: 765
Reputation: 4367
You are calling self
in a non-member function. There is no self
to reference in that context. my_Start
and my_Stop
should be members of myApp
:
class MyApp ...
def my_Stop(self, channel):
...
Upvotes: 1