<div dir="ltr"><div>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?</div>
<div> </div><div>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. </div><div> </div><div>while True:</div>
<div> QtCore.QTimer.singleShot(5000, lambda: self.updateLabel(voltageEdit))</div><div> </div><div>Thanks in advance</div><div>Best regards</div><div>Harry</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">
2013/7/26 Johoski, Timothy R <span dir="ltr"><<a href="mailto:timothy.r.johoski@intel.com" target="_blank">timothy.r.johoski@intel.com</a>></span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
> From: <a href="mailto:pyqt-bounces@riverbankcomputing.com">pyqt-bounces@riverbankcomputing.com</a> [mailto:<a href="mailto:pyqt-bounces@riverbankcomputing.com">pyqt-bounces@riverbankcomputing.com</a>] On Behalf Of ??<br>
> Sent: Friday, July 26, 2013 9:45 AM<br>
> To: <a href="mailto:pyqt@riverbankcomputing.com">pyqt@riverbankcomputing.com</a><br>
> Subject: [PyQt] How to change the value which is shown on GUI<br>
<div><div class="h5">><br>
> 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.<br>
><br>
> here is part of my code:<br>
><br>
> import sys<br>
> from PyQt4 import QtGui,QtCore<br>
> import time<br>
><br>
> class Example(QtGui.QWidget):<br>
><br>
> def __init__(self):<br>
> super(Example,self).__init__()<br>
><br>
> self.initUI()<br>
><br>
> def initUI(self):<br>
> voltage=12 #e.g. I get the value of voltage is 12v<br>
><br>
> voltageEdit=QtGui.QLabel(str(voltage),self)<br>
> voltageEdit.resize(voltageEdit.sizeHint())<br>
> voltageEdit.move(160,100)<br>
><br>
> time.sleep(5) # I want the GUI shows another value of voltage 5 seconds later<br>
><br>
> voltage=22<br>
> voltageEdit=QtGui.QLabel(str(voltage),self)<br>
> voltageEdit.resize(voltageEdit.sizeHint())<br>
> voltageEdit.move(160,100)<br>
><br>
> grid=QtGui.QGridLayout()<br>
> self.setLayout(grid)<br>
><br>
> self.setGeometry(300,200,600,400)<br>
> self.setWindowTitle("battery status")<br>
> self.show()<br>
><br>
> def main():<br>
><br>
> app=QtGui.QApplication(sys.argv)<br>
> ex=Example()<br>
> sys.exit(app.exec())<br>
><br>
> if __name__='__main__':<br>
> main()<br>
><br>
><br>
> When I executed this code, GUI shows the voltage is 12, and it will not change, please tell me how to fix it.<br>
><br>
> thanks in advance,<br>
><br>
> Harry<br>
<br>
</div></div>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).<br>
Hope this helps.<br>
- Tim<br>
<div class="im"><br>
import sys<br>
from PyQt4 import QtGui,QtCore<br>
import time<br>
<br>
class Example(QtGui.QWidget):<br>
<br>
def __init__(self):<br>
super(Example,self).__init__()<br>
<br>
self.initUI()<br>
<br>
def initUI(self):<br>
voltage=12 #e.g. I get the value of voltage is 12v<br>
<br>
voltageEdit=QtGui.QLabel(str(voltage),self)<br>
voltageEdit.resize(voltageEdit.sizeHint())<br>
voltageEdit.move(160,100)<br>
<br>
</div> # after 5 seconds (5000 milliseconds), call self.updateLabel<br>
QtCore.QTimer.singleShot(5000, lambda: self.updateLabel(voltageEdit))<br>
<div class="im"><br>
grid=QtGui.QGridLayout()<br>
self.setLayout(grid)<br>
<br>
self.setGeometry(300,200,600,400)<br>
self.setWindowTitle("battery status")<br>
self.show()<br>
<br>
</div> def updateLabel(self, voltageEdit):<br>
# change the following line to retrieve the new voltage from the device<br>
newvoltage=int(voltageEdit.text()) + 1<br>
voltageEdit.setText(str(newvoltage))<br>
QtCore.QTimer.singleShot(5000, lambda: self.updateLabel(voltageEdit))<br>
<br>
def main():<br>
<br>
app=QtGui.QApplication(sys.argv)<br>
ex=Example()<br>
sys.exit(app.exec_())<br>
<br>
if __name__=='__main__':<br>
main()<br>
</blockquote></div><br></div>