Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I stretch a single widget in a horizantal layout in PyQt4?

Here all three pushbutton are equivalent in size, how to increase size of only first pushbutton so that it occupies more space than other two buttons.

from PyQt4 import QtGui
import sys

class AllWidgets(QtGui.QWidget):

    def __init__(self):
        super(AllWidgets, self).__init__()
        layout = QtGui.QHBoxLayout()
        #code for pushbutton 1
        pushbutton_1 = QtGui.QPushButton()
        pushbutton_1.setText('First')
        layout.addWidget(pushbutton_1)
        #code for pushbutton 2
        pushbutton_2 = QtGui.QPushButton()
        pushbutton_2.setText('Second')
        layout.addWidget(pushbutton_2)
        #code for pushbutton 3
        pushbutton_3 = QtGui.QPushButton()
        pushbutton_3.setText('Third')
        layout.addWidget(pushbutton_3)
        self.setLayout(layout)

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    display = AllWidgets()
    display.show()
    sys.exit(app.exec_())
like image 796
user1994124 Avatar asked Mar 24 '23 00:03

user1994124


1 Answers

The second (optional) argument of addWigdet() is the strech factor. If you want first button to stretch, simply do:

layout.addWidget(pushbutton_1, 1)

If you want all buttons to stretch, but the first one to be bigger, you simply need to use different stretch factors:

layout.addWidget(pushbutton_1, 2)
layout.addWidget(pushbutton_2, 1)
layout.addWidget(pushbutton_3, 1)
like image 193
Charles Brunet Avatar answered Mar 26 '23 13:03

Charles Brunet