Imraan Kokkanti
Imraan Kokkanti

Reputation: 33

How to use statusBar().showMessage() from inside QWidget in PyQt5?

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?

Upvotes: 3

Views: 10468

Answers (1)

Mark Riddell
Mark Riddell

Reputation: 278

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

Upvotes: 5

Related Questions