开发者

How to have a PyQT program keep refreshing one widget, always providing an up-to-date value?

开发者 https://www.devze.com 2023-04-11 22:06 出处:网络
I\'m learning to program with PyQT4 and Pyt开发者_Go百科hon. I\'m trying to code a simple app that will display the current CPU usage in a QLCD widget. For CPU usage I\'m using psutils module.

I'm learning to program with PyQT4 and Pyt开发者_Go百科hon. I'm trying to code a simple app that will display the current CPU usage in a QLCD widget. For CPU usage I'm using psutils module.

The problem is that the CPU usage is not updated all the time - it only records the CPU usage at the moment the app has been launched (I'm guessing), and then it just stops. So, I'm looking for some sort of a loop equivalent that will hopefully not take too much of CPU power to process.

This is what I have so far:

self.wpCpuUsage.display(cpu_percent(interval=1))

and this is within __init__ of the QMainWindow class.

I've tried putting it in a for loop, but then it iterates over it, and basically waits for it to iterate and then executes the program.

Help?


You can use a QTimer[reference] object with a callback.

Something like that should work:

def call_this():
    self.wpCpuUsage.display(cpu_percent(interval=1))

self.my_timer = QtCore.QTimer()
self.my_timer.timeout.connect(call_this)
self.my_timer.start(1000) #1 second interval
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号