Search code examples
python-2.7pyqt4

How to adjust the space between the layout using pyqt4


Here is my code,i want to reduce the space between the layout.Can any one please tell me how can i reduce the space between the layouts. i tried many ways i used addStrech methos and set content margin also.But i didn't get the proper output.i dont have any idea where i did mistake.please guide me. Thank you in advance. Given below is my code:

class Example1(QtGui.QWidget):

    def __init__(self,typ):
        super(Example1, self).__init__()
        self.initUI()

    def initUI(self):

        self.grid2 = QtGui.QGridLayout()
        self.vbox1 = QtGui.QVBoxLayout()

        self.hbox3 = QtGui.QHBoxLayout()

        self.alignvbox = QtGui.QVBoxLayout()

        self.label = QtGui.QLabel("Payment Details")
        self.label.setStyleSheet("font: bold 30pt Comic Sans MS")
        self.hbox3.addWidget(self.label,QtCore.Qt.AlignTop)
        self.hbox3.addStretch()
        self.label2 = QtGui.QLabel("Amount : ")

        self.label2.setStyleSheet("font: bold 15pt Comic Sans MS")
        self.label3 = QtGui.QLabel("Quantity : ")
        self.label3.setStyleSheet("font: bold 15pt Comic Sans MS")
        self.amountvbox = QtGui.QVBoxLayout()
        self.amountvbox.setContentsMargins(0, 0, 0, 0)
        self.amountvbox.addWidget(self.label2)
        self.quantityvbox = QtGui.QVBoxLayout()
        self.quantityvbox.setContentsMargins(0, 0, 0, 0)
        self.quantityvbox.addWidget(self.label3)
        self.vbox1.addLayout(self.amountvbox)
        self.vbox1.addLayout(self.quantityvbox)

        self.hbox3.addLayout(self.vbox1,QtCore.Qt.AlignTop)

        self.alignvbox.addLayout(self.hbox3,QtCore.Qt.AlignTop)
        self.hbox4 = QtGui.QHBoxLayout()
        self.wbtn1 = QtGui.QPushButton("CASH")
        self.wbtn1.setStyleSheet(ROUNDED_STYLE_SHEET2)
        self.hbox4.addWidget(self.wbtn1)

        self.wbtn2 = QtGui.QPushButton("CARD")
        self.wbtn2.setStyleSheet(ROUNDED_STYLE_SHEET3)
        self.hbox4.addWidget(self.wbtn2)

        self.wbtn3 = QtGui.QPushButton("WALLET")
        self.wbtn3.setStyleSheet(ROUNDED_STYLE_SHEET4)
        self.hbox4.addWidget(self.wbtn3)
        self.alignvbox.addLayout(self.hbox4,QtCore.Qt.AlignTop)
        self.alignvbox.addStretch()

        self.vbox2 = QtGui.QVBoxLayout()

        self.label4 = QtGui.QLabel("Received Amount")
        self.vbox2.addWidget(self.label4)
        self.label4.setStyleSheet("font: bold 30pt Comic Sans MS")
        self.lb = QtGui.QLCDNumber()
        self.lb.setDigitCount(8)
        self.vbox2.addWidget(self.lb)
        self.vbox2.setAlignment(QtCore.Qt.AlignTop)
        self.hboxlayout = QtGui.QHBoxLayout()

        self.hboxlayout.addLayout(self.vbox2)
        self.hboxlayout.addStretch()
        self.layout = QtGui.QGridLayout()

        names = ['7', '8', '9',
                 '4', '5', '6',
                '1', '2', '3',
                 '<--', '0', '.']

        positions = [(i,j) for i in range(4) for j in range(3)]

        for position, name in zip(positions, names):
            button = QtGui.QPushButton(name)
            self.layout.addWidget(button, *position)
        self.hboxlayout.addLayout(self.layout)
        self.alignvbox.addLayout(self.hboxlayout)
        self.setLayout(self.alignvbox)
def main():
  app = QtGui.QApplication(sys.argv)
  ex = Example1()
  ex.show()
  ex.setGeometry(300,300,500,500)
  sys.exit(app.exec_())

Solution

  • One possible solution is to set the sizeHint as a fixed size after setting the layouts.

    import sys
    from PyQt4 import QtCore, QtGui
    
    QSS = '''
    QLabel#big{
        font: bold 30pt Comic Sans MS
    }
    QLabel#small{
        font: bold 15pt Comic Sans MS
    }
    '''
    
    class Example1(QtGui.QWidget):
        def __init__(self, parent=None):
            super(Example1, self).__init__(parent)
            self.initUI()
    
        def initUI(self):
            grid = QtGui.QGridLayout(self)
            label_payment = QtGui.QLabel("Payment Details", objectName="big")
            label_received = QtGui.QLabel("Received Amount", objectName="big")
    
            label_amount = QtGui.QLabel("Amount: 192", objectName="small")
            label_quantity = QtGui.QLabel("Quantity: 1", objectName="small")
    
            cash_button = QtGui.QPushButton("Cash")
            card_button = QtGui.QPushButton("Card")
            wallet_button = QtGui.QPushButton("Wallet")
    
            lcd = QtGui.QLCDNumber()
            sp = lcd.sizePolicy()
            sp.setHorizontalPolicy(QtGui.QSizePolicy.Fixed)
            lcd.setSizePolicy(sp)
    
            grid_buttons = QtGui.QGridLayout()
    
            names = ['7', '8', '9',
                     '4', '5', '6',
                    '1', '2', '3',
                     '<--', '0', '.']
            positions = [(i,j) for i in range(4) for j in range(3)]
            for position, name in zip(positions, names):
                button = QtGui.QPushButton(name)
                grid_buttons.addWidget(button, *position)
    
            grid.addWidget(label_payment, 0, 0, 2, 3)
            grid.addWidget(label_amount, 0, 5)
            grid.addWidget(label_quantity, 1, 5)
    
            hbox = QtGui.QHBoxLayout(spacing=0)
            hbox.setContentsMargins(0, 0, 0, 0)
            hbox.addWidget(cash_button)
            hbox.addWidget(card_button)
            hbox.addWidget(wallet_button)
            grid.addLayout(hbox, 3, 0, 1, 6)
    
            vbox = QtGui.QVBoxLayout(spacing=0)
            vbox.setContentsMargins(0, 0, 0, 0)
            vbox.addWidget(label_received)
            vbox.addWidget(lcd)
            grid.addLayout(vbox, 4, 1)
    
            grid.addLayout(grid_buttons, 4, 3, 4, 3)
            self.setFixedSize(self.sizeHint())
    
    def main():
        app = QtGui.QApplication(sys.argv)
        app.setStyleSheet(QSS)
        ex = Example1()
        ex.show()
        ex.setGeometry(300,300,500,500)
        sys.exit(app.exec_())
    
    if __name__ == '__main__': main()
    

    enter image description here