2017-10-25 36 views
0

계속 진행하고 싶은 3 개의 진행 표시 줄이있는 간단한 대화 상자가 있습니다 (시스템 리소스 사용 표시). 문서 주위를 읽으면서 QTimerx 밀리 초 (진행률 막대를 업데이트 함)마다 기능을 시작하는 올바른 방법입니다. 그러나, 나는 그것을 작동시킬 수 없으며, 나는 왜 그런지 알지 못합니다. 타이머 타임 아웃 신호를 업데이트 기능에 연결하는 것이 상대적으로 간단 해 보이지만 결코 실행되지는 않습니다. 여기 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

내가 필요로 그냥 뭐, 감사 ! 그것의 거의 항상 간단한 무언가. 이제 쓰레기 수거를 공부하는 동안 실례합니다. (기본적인 파이썬에 대한 책을 읽어야합니다.) – Spencer