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:jiwen0304 at gmail.com]
Sent: Friday, July 26, 2013 2:54 PM
To: Johoski, Timothy R
Cc: pyqt at 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.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()
More information about the PyQt
mailing list
‘She has never mentioned her father to me. Was he—well, the sort of man whom the County Club would not have blackballed?’ "We walked by the side of our teams or behind the wagons, we slept on the ground at night, we did our own cooking, we washed our knives by sticking them into the ground rapidly a few times, and we washed our plates with sand and wisps of grass. When we stopped, we arranged our wagons in a circle, and thus formed a 'corral,' or yard, where we drove our oxen to yoke them up. And the corral was often very useful as a fort, or camp, for defending ourselves against the Indians. Do you see that little hollow down there?" he asked, pointing to a depression in the ground a short distance to the right of the train. "Well, in that hollow our wagon-train was kept three days and nights by the Indians. Three days and nights they stayed around, and made several attacks. Two of our men were killed and three were wounded by their arrows, and others had narrow escapes. One arrow hit me on the throat, but I was saved by the knot of my neckerchief, and the point only tore the skin a little. Since that time I have always had a fondness for large neckties. I don't know how many of the Indians we killed, as they carried off their dead and wounded, to save them from being scalped. Next to getting the scalps of their enemies, the most important thing with the Indians is to save their own. We had several fights during our journey, but that one was the worst. Once a little party of us were surrounded in a small 'wallow,' and had a tough time to defend ourselves successfully. Luckily for us, the Indians had no fire-arms then, and their bows and arrows were no match for our rifles. Nowadays they are well armed, but there are[Pg 41] not so many of them, and they are not inclined to trouble the railway trains. They used to do a great deal of mischief in the old times, and many a poor fellow has been killed by them." As dusk came on nearly the whole population of Maastricht, with all their temporary guests, formed an endless procession and went to invoke God's mercy by the Virgin Mary's intercession. They went to Our Lady's Church, in which stands the miraculous statue of Sancta Maria Stella Maris. The procession filled all the principal streets and squares of the town. I took my stand at the corner of the Vrijthof, where all marched past me, men, women, and children, all praying aloud, with loud voices beseeching: "Our Lady, Star of the Sea, pray for us ... pray for us ... pray for us ...!" It had not occurred to her for some hours after Mrs. Campbell had told her of Landor's death that she was free now to give herself to Cairness. She had gasped, indeed, when she did remember it, and had put the thought away, angrily and self-reproachfully. But it returned now, and she felt that she might cling to it. She had been grateful, and she had been faithful, too.[Pg 286] She remembered only that Landor had been kind to her, and forgot that for the last two years she had borne with much harsh coldness, and with a sort of contempt which she felt in her unanalyzing mind to have been entirely unmerited. Gradually she raised herself until she sat quite erect by the side of the mound, the old exultation of her half-wild girlhood shining in her face as she planned the future, which only a few minutes before had seemed so hopeless. After he had gloated over Sergeant Ramsey, Shorty got his men into the road ready to start. Si placed himself in front of the squad and deliberately loaded his musket in their sight. Shorty took his place in the rear, and gave out: The groups about each gun thinned out, as the shrieking fragments of shell mowed down man after man, but the rapidity of the fire did not slacken in the least. One of the Lieutenants turned and motioned with his saber to the riders seated on their horses in the line of limbers under the cover of the slope. One rider sprang from each team and ran up to take the place of men who had fallen. "As long as there's men and women in the world, the men 'ull be top and the women bottom." Then, in the house, the little girls were useful. Mrs. Backfield was not so energetic as she used to be. She had never been a robust woman, and though her husband's care had kept her well and strong, her frame was not equal to Reuben's demands; after fourteen years' hard labour, she suffered from rheumatism, which though seldom acute, was inclined to make her stiff and slow. It was here that Caro and Tilly came in, and Reuben began to appreciate his girls. After all, girls were needed in a house—and as for young men and marriage, their father could easily see that such follies did not spoil their usefulness or take them from him. Caro and Tilly helped their grandmother in all sorts of ways—they dusted, they watched pots, they shelled peas and peeled potatoes, they darned house-linen, they could even make a bed between them. HoME一级毛片视频免费公开
ENTER NUMBET 00185yzc.com.cn www.omnius.com.cn jcdcmall.com.cn yangyuzhou.com.cn www.wellshield.com.cn www.panjo.com.cn zeugma.com.cn rtdv.com.cn bjyzly.com.cn prcpe.com.cn