hi Harry,

At the end of the updateLabel() function, it calls QTimer.singleShot to ensure 
that Qt will call updateLabel again in another 5 seconds.  So, the sequence of 
operations is something like this:
  - at the bottom of the file, the main function is called
  - main calls initUI
  - initUI constructs the widgets and calls QTimer.singleShot to schedule 
updateLabel to be called in 5 seconds
  - initUI returns back to main
  - main calls QApplication.exec_ to bring up the gui
  - QApplication.exec_ enters a loop to process mouse, button, key, and timer 
events
  - after 5 seconds, the QTimer expires and Qapplication's event loop calls 
updateLabel
  - updateLabel checks the voltage, current temperature, etc and updates the 
gui as appropriate
  - updateLabel calls QTimer.singleShot to schedule updateLabel to be called in 
another 5 seconds
  - control returns back to Qapplication's event loop and it processes mouse, 
button, key, and timer events again...  and after 5 seconds, the timer expires 
and calls updateLabel again.  and so on...

So, in essence, the updateLabel function is equivalent to your while True loop, 
it ensures that updateLabel is called every 5 seconds.  If you have other 
values like current temperature, etc, I recommend updating those values in the 
updateLabel function.  Hope this answers your questions..  Good luck.

- Tim



From: 吉文 [mailto:jiwen0...@gmail.com] 
Sent: Friday, July 26, 2013 2:54 PM
To: Johoski, Timothy R
Cc: pyqt@riverbankcomputing.com
Subject: Re: [PyQt] How to change the value which is shown on GUI

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.joho...@intel.com>
> From: pyqt-boun...@riverbankcomputing.com 
> [mailto:pyqt-boun...@riverbankcomputing.com] On Behalf Of ??
> Sent: Friday, July 26, 2013 9:45 AM
> To: pyqt@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()

_______________________________________________
PyQt mailing list    PyQt@riverbankcomputing.com
http://www.riverbankcomputing.com/mailman/listinfo/pyqt

Reply via email to