Positioning of widgets in PyQt4

A short note on positioning of widgets in PyQt4 .

Let say you have a layout , a button and few labels. If I press a button and a label should become invisible , your code should look something like this.

class Window(QtGui.QWidget):
    def __init__(self):
        super(Window , self).__init__()
        <code>layout = QtGui.QGridLayout(self)
        self.label1 = QtGui.QLabel('Label1')
        label2 = QtGui.QLabel('Label2')
        label3 = QtGui.QLabel('Label3')
        layout.addWidget(self.label1 , 0 , 0)
        layout.addWidget(label2 , 1 , 0)
        layout.addWidget(label3 , 2 , 0)
        button = QtGui.QPushButton('Hide')

    def fun(self):

The problem with this however is , it automatically repositions your labels such that it occupies the entire window. In order to prevent this , its better to use a frame.

//In your __init__ constructor.
frame = QtGui.QFrame()
self.label1 = QtGui.QLabel(frame)

This makes sure the labels dont get repositioned.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: