简体   繁体   中英

How to properly use setRowStretch in PyQt5

I am trying to use setRowStretch to stretch my rows of button vertically inside a QGridLayout but for some reasons it doesn't want to work. Interestingly, setColumnStretch is working for me, however. I'm learning to use PyQt5 so there may be something I still don't know about. See attached for the screenshot of my application. Please help.

import PyQt5.QtWidgets as qtw
import PyQt5.QtGui as qtg
import PyQt5.QtCore as qtcore

class MainWindow(qtw.QWidget):
    def __init__(self):
        super().__init__()
            # Add a title
        self.setWindowTitle('Calculator - Anh Nguyen')
        
        
        # Create a label
        my_label = qtw.QLabel("0")
        # Set background color
        my_label.setStyleSheet("background-color: #e3e1da;\
                                border: 1px solid black")
        
        my_label.setMaximumHeight(50)
        
        # Align label text
        my_label.setAlignment(qtcore.Qt.AlignmentFlag.AlignVCenter | qtcore.Qt.AlignmentFlag.AlignRight)        
        
        my_label.setFont(qtg.QFont('Helvetica', 20))
        
        # Add grid layout for calculator buttons
        main_gridlayout = qtw.QGridLayout()
        
        main_gridlayout.addWidget(my_label, 0, 0, 1, 4)
        main_gridlayout.addWidget(qtw.QPushButton('%'), 1, 0)
        main_gridlayout.addWidget(qtw.QPushButton('CE'), 1, 1)
        main_gridlayout.addWidget(qtw.QPushButton('C'), 1, 2)
        main_gridlayout.addWidget(qtw.QPushButton('BKSP'), 1, 3)
        main_gridlayout.addWidget(qtw.QPushButton('7'), 2, 0)
        main_gridlayout.addWidget(qtw.QPushButton('8'), 2, 1)
        main_gridlayout.addWidget(qtw.QPushButton('9'), 2, 2)
        main_gridlayout.addWidget(qtw.QPushButton('/'), 2, 3)
        main_gridlayout.addWidget(qtw.QPushButton('4'), 3, 0)
        main_gridlayout.addWidget(qtw.QPushButton('5'), 3, 1)
        main_gridlayout.addWidget(qtw.QPushButton('6'), 3, 2)
        main_gridlayout.addWidget(qtw.QPushButton('-'), 3, 3)
        main_gridlayout.addWidget(qtw.QPushButton('1'), 4, 0)
        main_gridlayout.addWidget(qtw.QPushButton('2'), 4, 1)
        main_gridlayout.addWidget(qtw.QPushButton('3'), 4, 2)
        main_gridlayout.addWidget(qtw.QPushButton('+'), 4, 3)
        main_gridlayout.addWidget(qtw.QPushButton('+/-'), 5, 0)
        main_gridlayout.addWidget(qtw.QPushButton('0'), 5, 1)
        main_gridlayout.addWidget(qtw.QPushButton('.'), 5, 2)
        main_gridlayout.addWidget(qtw.QPushButton('='), 5, 3)
        
        # This is NOT working:
        main_gridlayout.setRowStretch(1,1)
        
        # This is working
        main_gridlayout.setColumnStretch(2,1)
        
        
        
        self.setLayout(main_gridlayout)
        
        self.show()
        
app = qtw.QApplication([])
mw = MainWindow()

app.exec_()

enter image description here

QPushButtons size policy by default are set to take up as much horizontal space as possible. If you want the same to be true vertically then you will need to adjust the buttons size policy.

for example

btn = qtw.QPushButton('%')
policy = btn.sizePolicy()
policy.setVerticalPolicy(policy.Policy.Minimum)  # minimum means grow 
                                                 # dont ask me why
btn.setSizePolicy(Policy)

Seeing as you have so many buttons though I would suggest subclassing QPushButton and set the default in __init__ rather than set the policy on each individual button.

class Button(qtw.QPushButton):

   def __init__(self, text, parent=None):
       super().__init__(text, parent=parent)
       policy = self.sizePolicy()
       policy.setVerticalPolicy(policy.Policy.Minimum)
       self.setSizePolicy(policy)

then when creating you buttons in the main window

btn = Button("%")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM