2017-10-25 4 views
0

私は継続的に更新したい(システムリソースの使用状況を表示する)3つの進捗バーを持つ簡単なダイアログを持っています。ドキュメントを読むことから、は、xミリ秒ごとにファンクションを起動する正しい方法です(これにより、進行状況バーが更新されます)。しかし、私はそれを働かせることができず、私はなぜそれほど知りません。タイマーのタイムアウト信号を更新機能に接続するのは比較的簡単ですが、決して起動するようには見えません。ここでPyQt4:QTimerを使用して進捗バーを継続的に更新します

は私のコードです:

import sys 
from PyQt4 import QtGui, QtCore 
import psutil 

class Tiny_System_Monitor(QtGui.QWidget): 
    def __init__(self): 
     super(Tiny_System_Monitor, self).__init__() 
     self.initUI() 

    def initUI(self): 
     mainLayout = QtGui.QHBoxLayout() 

     self.cpu_progressBar = QtGui.QProgressBar() 
     self.cpu_progressBar.setTextVisible(False) 
     self.cpu_progressBar.setOrientation(QtCore.Qt.Vertical) 
     mainLayout.addWidget(self.cpu_progressBar) 

     self.vm_progressBar = QtGui.QProgressBar() 
     self.vm_progressBar.setOrientation(QtCore.Qt.Vertical) 
     mainLayout.addWidget(self.vm_progressBar) 

     self.swap_progressBar = QtGui.QProgressBar() 
     self.swap_progressBar.setOrientation(QtCore.Qt.Vertical) 
     mainLayout.addWidget(self.swap_progressBar) 

     self.setLayout(mainLayout) 

     timer = QtCore.QTimer() 
     timer.timeout.connect(self.updateMeters) 
     timer.start(1000) 

    def updateMeters(self): 
     cpuPercent = psutil.cpu_percent() 
     vmPercent = getattr(psutil.virtual_memory(), "percent") 
     swapPercent = getattr(psutil.swap_memory(), "percent") 

     self.cpu_progressBar.setValue(cpuPercent) 
     self.vm_progressBar.setValue(vmPercent) 
     self.swap_progressBar.setValue(swapPercent) 
     print "updated meters" 

def main(): 
    app = QtGui.QApplication(sys.argv) 
    ex = Tiny_System_Monitor() 

    ex.show() 
    sys.exit(app.exec_()) 

if __name__ == '__main__': 
    main() 

答えて

1

あなたはinitUI戻ったときに、それ以外の場合は、すぐにガベージコレクトされます、タイマーオブジェクトへの参照を保持する必要があります。

class Tiny_System_Monitor(QtGui.QWidget): 
    ... 
    def initUI(self): 
     ...  
     self.timer = QtCore.QTimer() 
     self.timer.timeout.connect(self.updateMeters) 
     self.timer.start(1000) 
+0

は私が必要なものだけ、ありがとう!ほとんど常にシンプルなものです。ガベージコレクションで勉強しているうちに私を許​​してください...(私は基本的なPythonの本を読む必要があります) – Spencer

関連する問題