Search code examples
pythonmultithreadingtimerpyqt4self-updating

updating a value every n seconds


I'm creating a real-time application(on a Raspberry Pi) in Python and I have a problem concerning "Updating a value in the program every 5 seconds". I'm using Python 2.7.9 as interpreter and for the GUI programming: PyQt4.

I must do a request to a measuring instrument and I get One value from this instrument. I want to store this value every 5 seconds. But I don't want to program to wait because it must do other things. An infinite while-loop is not possible. This is the code of my main program:

class ApplicationWindow(QtGui.QMainWindow):
    def __init__(self):

        QtGui.QMainWindow.__init__(self)
        self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
        self.setWindowTitle("application main window")
        self.setStyleSheet('background-color:#DBE0E4')
        self.file_menu = QtGui.QMenu('&File', self)
        self.file_menu.addAction('&Quit', self.fileQuit,
                              QtCore.Qt.CTRL + QtCore.Qt.Key_Q)
        self.menuBar().addMenu(self.file_menu)

        self.help_menu = QtGui.QMenu('&Help', self)
        self.menuBar().addSeparator()
        self.menuBar().addMenu(self.help_menu)

        self.help_menu.addAction('&About', self.about)

        self.instrument=Online_meter('name','pasword')

        timer=QtCore.QTimer()
        timer.start(5000)
        timer.timeout.connect(instrument.update())



        self.main_widget = QtGui.QWidget(self)
        layout=QtGui.QGridLayout(self.main_widget)
        layout.setSpacing(10)
        layout.expandingDirections()

        time=Datetime() 
        dc = Plotgraph(self.main_widget)
        label1=Label(" Value:",False)
        label2=Label("waarde",True)

        layout.addWidget(dc,1,1,8,7)
        layout.addWidget(time,1,8,1,2)
        layout.addWidget(label1,2,8,1,2)
        layout.addWidget(label2,3,8,1,2)

        self.main_widget.setFocus()
        self.setCentralWidget(self.main_widget)



   def fileQuit(self):
       self.close()

   def closeEvent(self, ce):
       self.fileQuit()

   def about(self):
       QtGui.QMessageBox.about(self, "About",
        """
       Copyright 2014 
       """

      )


qApp = QtGui.QApplication(sys.argv)

aw = ApplicationWindow()
aw.setWindowTitle("my app")
aw.showFullScreen()
sys.exit(qApp.exec_())

And here follows the code for my method for updating the Online meter:

   def update(self):


        waarden=self.post_request(meter)
        self.data=[datetime.now(),values[6]]

here I tried to use a Qtimer. I tought this would work but it doensn't. I get the following error:

TypeError: connect() slot argument should be a callable or a signal, not 'Nonetype'

I don't know how the fix it. I tought about threading but I think this won't be good for my CPU usage of the RPi. Does anyone know a good solution to my problem?

thanks in advance


Solution

  • This:

    timer.timeout.connect(instrument.update())
    

    should probably be

    timer.timeout.connect(instrument.update)
    

    The former calls the function immediately while executing that line; the function returns None, which causes error in .connect. The latter just connects that function to the timeout slot.

    The first invocation of instrument.update should happen after 5 seconds