【问题标题】:Updating Pyqt Status Bar widget更新 Pyqt 状态栏小部件
【发布时间】:2012-07-25 12:43:08
【问题描述】:

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

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_())

【问题讨论】:

    标签: python qt pyqt


    【解决方案1】:

    你有这个代码:

        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()
    

    【讨论】:

      猜你喜欢
      • 2021-06-11
      • 2018-09-01
      • 1970-01-01
      • 2021-07-25
      • 2022-11-12
      • 2011-07-12
      • 1970-01-01
      • 1970-01-01
      • 2016-10-04
      相关资源
      最近更新 更多