[PyQt] How to change the value which is shown on GUI

吉文 jiwen0304 at gmail.com
Fri Jul 26 22:53:56 BST 2013


Hi, Tim, Thank you very much for helping me, I will try it tomorrow. If
there is other value such as current, temperature etc. , this value like
the voltage, is also the real changing data, how to realize this function?
Calling updateLabel() many times?

One more question, the voltage will change in every 5 seconds, i.e.
raspberrypi will receive the value of voltage every 5 seconds, can I write
the code like the following.

while True:
          QtCore.QTimer.singleShot(5000, lambda:
self.updateLabel(voltageEdit))

Thanks in advance
Best regards
Harry


2013/7/26 Johoski, Timothy R <timothy.r.johoski at intel.com>

> > From: pyqt-bounces at riverbankcomputing.com [mailto:
> pyqt-bounces at riverbankcomputing.com] On Behalf Of ??
> > Sent: Friday, July 26, 2013 9:45 AM
> > To: pyqt at riverbankcomputing.com
> > Subject: [PyQt] How to change the value which is shown on GUI
> >
> > Hello list, I want to design a battery monitoring system via
> raspberrypi. The voltage of battery is real changing data, I try to write
> some code, but the voltage can not change when I change the value of
> voltage.
> >
> > here is part of my code:
> >
> > import sys
> > from PyQt4 import QtGui,QtCore
> > import time
> >
> > class Example(QtGui.QWidget):
> >
> >         def __init__(self):
> >               super(Example,self).__init__()
> >
> >               self.initUI()
> >
> >          def initUI(self):
> >                voltage=12 #e.g. I get the value of voltage is 12v
> >
> >                voltageEdit=QtGui.QLabel(str(voltage),self)
> >                voltageEdit.resize(voltageEdit.sizeHint())
> >                voltageEdit.move(160,100)
> >
> >                time.sleep(5) # I want the GUI shows another value of
> voltage 5 seconds later
> >
> >                voltage=22
> >                voltageEdit=QtGui.QLabel(str(voltage),self)
> >                voltageEdit.resize(voltageEdit.sizeHint())
> >                voltageEdit.move(160,100)
> >
> >                grid=QtGui.QGridLayout()
> >                self.setLayout(grid)
> >
> >                self.setGeometry(300,200,600,400)
> >                self.setWindowTitle("battery status")
> >                self.show()
> >
> > def main():
> >
> >          app=QtGui.QApplication(sys.argv)
> >          ex=Example()
> >          sys.exit(app.exec())
> >
> > if __name__='__main__':
> >          main()
> >
> >
> > When I executed this code, GUI shows the voltage is 12, and it will not
> change, please tell me how to fix it.
> >
> > thanks in advance,
> >
> > Harry
>
> hi Harry, the following illustrates how to update the label every 5
> seconds using a QTimer.  In the original code, time.sleep(5) will actually
> put the application to sleep and Qt will not process any events (mouse
> motion, button presses, repaint events, etc).  But in the code below,
> QTimer.singleShot "schedules" the updateLabel function to be called in 5
> seconds.  QTimer.singleShot returns immediately, allowing the next lines of
> code to run and show the widget on the screen.  5 seconds later, Qt calls
> updateLabel which changes the voltage text (and then schedules another call
> to itself 5 more seconds from now).
> Hope this helps.
> - Tim
>
> import sys
> from PyQt4 import QtGui,QtCore
> import time
>
> class Example(QtGui.QWidget):
>
>         def __init__(self):
>               super(Example,self).__init__()
>
>               self.initUI()
>
>         def initUI(self):
>               voltage=12 #e.g. I get the value of voltage is 12v
>
>               voltageEdit=QtGui.QLabel(str(voltage),self)
>               voltageEdit.resize(voltageEdit.sizeHint())
>               voltageEdit.move(160,100)
>
>               # after 5 seconds (5000 milliseconds), call self.updateLabel
>               QtCore.QTimer.singleShot(5000, lambda:
> self.updateLabel(voltageEdit))
>
>               grid=QtGui.QGridLayout()
>               self.setLayout(grid)
>
>               self.setGeometry(300,200,600,400)
>               self.setWindowTitle("battery status")
>               self.show()
>
>         def updateLabel(self, voltageEdit):
>               # change the following line to retrieve the new voltage from
> the device
>               newvoltage=int(voltageEdit.text()) + 1
>               voltageEdit.setText(str(newvoltage))
>               QtCore.QTimer.singleShot(5000, lambda:
> self.updateLabel(voltageEdit))
>
> def main():
>
>          app=QtGui.QApplication(sys.argv)
>          ex=Example()
>          sys.exit(app.exec_())
>
> if __name__=='__main__':
>          main()
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.riverbankcomputing.com/pipermail/pyqt/attachments/20130726/b6199d4a/attachment.html>


More information about the PyQt mailing list