Updating Pyqt Status Bar widget
pyqt, python, qt
Solution
You have this code:
self.a = QtGui.QStatusBar.showMessage("System Status | Normal")
ver.addWidget(self.a)
`showMessage` is not a class method, you need a `QStatusBar` instance for it. I think you wanted to do this:
self.a = QtGui.QStatusBar(self)
ver.addWidget(self.a)
self.a.showMessage("System Status | Normal")
Maybe it would be easier to subclass `QMainWindow`? Then you could use `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()
Problem
How can I update the status bar widget? Also How can i use signals and threads instead of a button? Thanks! Can someone help me out, my code is not working, when i press the button nothing comes up, i also get an error: ``` 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_()) ```