4

如何更新状态栏小部件?另外我如何使用信号和线程而不是按钮?谢谢!谁能帮帮我,我的代码不起作用,当我按下按钮时没有任何反应,我也收到错误消息:

Error:
    self.a = QtGui.QStatusBar.showMessage("System Status | Normal")
    TypeError: QStatusBar.showMessage(QString, int msecs=0): first argument of unbound method must have type 'QStatusBar'

from PyQt4 import QtGui,QtCore
import sys

class main_window(QtGui.QWidget):
    def __init__(self,parent=None):
        #Layout       
        QtGui.QWidget.__init__(self,parent)
        self.bt=QtGui.QPushButton('crash')
        self.lbl=QtGui.QLabel('count')
        ver=QtGui.QHBoxLayout(self)
        ver.addWidget(self.bt)

        self.cnt=0
        self.running=False
        self.connect(self.bt,QtCore.SIGNAL("clicked()"),self.count)

        self.a = QtGui.QStatusBar.showMessage("System Status | Normal")
        ver.addWidget(self.a)
        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.count)
        # check every second
        self.timer.start(1000*1)       

    def count(self):
        a = open("connection_cpu.txt","r")
        if a == "CPU Overclocked":
            abnormal_label = QtGui.QLabel("System Status | Normal")  
            abnormal_label.setStyleSheet(' QLabel {color: red}')
            QtGui.QStatusBar.addWidget(abnormal_label)
            self.repaint()
        else:
            normal_label = QtGui.QLabel("System Status | Normal")
            QtGui.QStatusBar.addWidget(normal_label)
            self.repaint()




if __name__ == '__main__':

    app = QtGui.QApplication(sys.argv)
    mw=main_window()
    mw.show()
    sys.exit(app.exec_())
4

1 回答 1

9

你有这个代码:

    self.a = QtGui.QStatusBar.showMessage("System Status | Normal")
    ver.addWidget(self.a)

showMessage不是类方法,您需要一个QStatusBar实例。我想你想这样做:

    self.a = QtGui.QStatusBar(self)
    ver.addWidget(self.a)
    self.a.showMessage("System Status | Normal")

也许子类化会更容易QMainWindow?然后你可以使用QMainWindow.statusBar

class MainWindow(QtGui.QMainWindow):

    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)

        self.bt = QtGui.QPushButton('crash')
        self.lbl = QtGui.QLabel('count')

        self.cnt = 0
        self.running = False

        self.bt.clicked.connect(self.count) # new style signal/slot connection

        # http://doc.qt.nokia.com/4.7-snapshot/qmainwindow.html#statusBar
        self.statusBar().showMessage("System Status | Normal") 

        #Layout
        vert_layout = QtGui.QHBoxLayout()
        vert_layout.addWidget(self.bt)
        self.main_widget = QtGui.QWidget(self)
        self.main_widget.setLayout(vert_layout)
        self.setCentralWidget(self.main_widget)

        self.timer = QtCore.QTimer()
        self.timer.timeout.connect(self.count)
        # check every second
        self.timer.start(1000*1)


    def count(self):
        a = open("connection_cpu.txt", "r").read()
        if a == "CPU Overclocked":
            abnormal_label = QtGui.QLabel("System Status | Normal")  
            abnormal_label.setStyleSheet(' QLabel {color: red}')
            self.statusBar().addWidget(abnormal_label)
            self.repaint()
        else:
            normal_label = QtGui.QLabel("System Status | Normal")
            self.statusBar().addWidget(normal_label)
            self.repaint()
于 2012-07-27T07:51:02.347 回答