Click here to Skip to main content
15,887,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I am currently learning python by trying to create a GUI with PyQt5, and i'm not very good at it. So i went started going through tutorials at PyQt5 tutorial - learn GUI programming with PyQt5[^] and started combining the some of the sections of code to understand it more.

Then i got to then i got to the layout management section encountered a problem of not being able to use QGridLayout and QStatusBar at the same time. I saw somewhere that this can be solved with setCentralWidget() but I couldn't figure out how to use it and couldn't find anything online.

What I have tried:

here is what i have so far. The idea is to get the program to tell you what button you pressed in the status bar.

Python
import sys
from PyQt5.QtWidgets import QWidget, QPushButton, QApplication, QGridLayout

class Problem(QWidget):
    def __init__(self):
        super().__init__()

        self.initUI()

    def initUI(self):

        grid = QGridLayout()
        grid.setSpacing(10)


        names = [['Cls', 'Bck', '', 'Close'],
                 ['7', '8', '9', '/'],
                 ['4', '5', '6', '*'],
                 ['1', '2', '3', '-'],
                 ['0', '.', '=', '+']]
        row_num = 0
        column_num = 0
        for row in names :
            row_num += 1
            column_num = 0
            for column in row:
                column_num += 1
                button = QPushButton(column)
                grid.addWidget(button, row_num,column_num)
#                button.clicked.connect(self.buttonClicked)

#            self.statusBar()
            self.setLayout(grid)


            self.setGeometry(300,300,290,150)
            self.setWindowTitle("not working")
            self.show()

    #def buttonClicked(self):

#        sender = self.sender()
#        self.statusBar().showMessage(sender.text() + ' was pressed')






if __name__ == '__ma<pre>
in__':
app = QApplication(sys.argv)
ex = Problem()
sys.exit(app.exec_())

The code as it is should produce a window with a calculator like layout. Take out the comments and i get the error
"Problem" has no attribute "statusBar"

I heard that i could use setCentralWidget() but i'm not sure how it works. Couldn't find much about it.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900