2

I am trying to learn PyQt5/Python 3.6.3. I was trying to show a message in the statusbar when a button is clicked. The problem is that the said buttons are inside a QWidget and as far as I could find, statusBar() is only available in QMainWindow. This is the code I pieced together so far...

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets  import QStatusBar, QMainWindow, QApplication, QWidget,QHBoxLayout, QVBoxLayout, QPushButton, QSlider, QLCDNumber, QLabel

class MyMainWindow(QMainWindow):

    def __init__(self, parent=None):
        super().__init__()
        self.main_widget = FormWidget(self)
        self.setCentralWidget(self.main_widget)
        self.init_UI()

    def init_UI(self):
        self.statusBar().showMessage('Ready')
        self.setGeometry(200, 100, 300, 300)
        self.setWindowTitle('Central Widget')
        self.show()

class FormWidget(QWidget):

    def __init__(self, parent):
        super(FormWidget, self).__init__(parent)
        self.init_UI()

    def init_UI(self):
        hbox = QHBoxLayout()
        button_1 = QPushButton('Button 1', self)
        button_1.clicked.connect(self.buttonClicked)
        hbox.addWidget(button_1)
        button_2 = QPushButton('Button 2', self)
        button_2.clicked.connect(self.buttonClicked)
        hbox.addWidget(button_2)
        self.setLayout(hbox)
        self.setGeometry(200, 100, 300, 300)
        self.setWindowTitle('Slider and LCD')
        self.show()

    def buttonClicked(self):
        sender = self.sender()
        self.statusBar.showMessage(sender.text() + ' was clicked')

if __name__ == '__main__':
    APP = QApplication(sys.argv)
    ex = MyMainWindow()
    sys.exit(APP.exec_())

When I run it I get the following error:

Traceback (most recent call last):
  File "central_widget_test.py", line 40, in buttonClicked
    self.statusBar.showMessage(sender.text() + ' was clicked')
AttributeError: 'FormWidget' object has no attribute 'statusBar'

Can someone please help me solve this?

1 Answer 1

5

You should initialise a statusbar object on your MyMainWindow class which can be updated in the future.

Your FormWidget can then update the statusbar by referencing the MyMainWindow object.

class MyMainWindow(QMainWindow):

    . . .

    def init_UI(self):
        self.statusbar = self.statusBar()
        self.statusbar.showMessage('Ready')
        self.setGeometry(200, 100, 300, 300)
        self.setWindowTitle('Central Widget')
        self.show()

class FormWidget(QWidget):

    def __init__(self, parent):
        super(FormWidget, self).__init__(parent)
        self.parent = parent
        self.init_UI()

    . . .

     def buttonClicked(self):
        sender = self.sender()
        self.parent.statusbar.showMessage(sender.text() + ' was clicked')
1
  • is self.statusbar = self.statusBar() actually useful? Does it cost more to call the function from class FormWidget than to access the variable?
    – mins
    May 10, 2021 at 10:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.