Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Python PyQt Layout Management

Python PyQt Layout Management

In GUI application development,layout managementrefers to how the various widgets in a window (such as buttons and text boxes) are organized and arranged.

PyQt provides several layout managers that automatically adjust widget positions and sizes so that the interface looks good at different window sizes.

Why layout management is needed

  1. Adapt to different resolutions: Users may have screens of different sizes
  2. Simplified Development: No need to manually calculate the position of every widget
  3. Easy to maintain: When widgets are added or removed, the layout adjusts automatically

Main layout managers in PyQt

PyQt provides the following commonly used layout managers:

1. QHBoxLayout - horizontal layout

Arranges widgets horizontally from left to right.

Example

layout

=

QHBoxLayout

(

)

layout.

addWidget

(

QPushButton

(

"Button 1"

)

)

layout.

addWidget

(

QPushButton

(

"Button 2"

)

)

layout.

addWidget

(

QPushButton

(

"Button 3"

)

)

2. QVBoxLayout - vertical layout

Arranges widgets vertically from top to bottom.

Example

layout

=

QVBoxLayout

(

)

layout.

addWidget

(

QPushButton

(

"Button 1"

)

)

layout.

addWidget

(

QPushButton

(

"Button 2"

)

)

layout.

addWidget

(

QPushButton

(

"Button 3"

)

)

3. QGridLayout - grid layout

Arranges widgets in the rows and columns of a grid.

Example

layout

=

QGridLayout

(

)

layout.

addWidget

(

QPushButton

(

"1,1"

)

,

0

,

0

)

 

Row 0, column 0

layout.

addWidget

(

QPushButton

(

"1,2"

)

,

0

,

1

)

 

Row 0, column 1

layout.

addWidget

(

QPushButton

(

"2,1"

)

,

1

,

0

)

 

Row 1, column 0

4. QFormLayout - form layout

Especially suited to form interfaces made of label-field pairs.

Example

layout

=

QFormLayout

(

)

layout.

addRow

(

"Name:"

,

QLineEdit

(

)

)

layout.

addRow

(

"Age:"

,

QLineEdit

(

)

)


Nesting layouts

In real applications we often need to combine several layout managers to build a complex interface.

Example: main window layout

Example

Create the main window

window

=

QWidget

(

)

Create the main vertical layout

main_layout

=

QVBoxLayout

(

)

Add a top horizontal layout

top_layout

=

QHBoxLayout

(

)

top_layout.

addWidget

(

QPushButton

(

"File"

)

)

top_layout.

addWidget

(

QPushButton

(

"Edit"

)

)

main_layout.

addLayout

(

top_layout

)

Add a central grid layout

center_layout

=

QGridLayout

(

)

center_layout.

addWidget

(

QTextEdit

(

)

,

0

,

0

,

2

,

2

)

 

Occupies 2 rows and 2 columns

center_layout.

addWidget

(

QPushButton

(

"Save"

)

,

0

,

2

)

center_layout.

addWidget

(

QPushButton

(

"Cancel"

)

,

1

,

2

)

main_layout.

addLayout

(

center_layout

)

Add a bottom horizontal layout

bottom_layout

=

QHBoxLayout

(

)

bottom_layout.

addStretch

(

)

 

Add stretchable space

bottom_layout.

addWidget

(

QPushButton

(

"OK"

)

)

bottom_layout.

addWidget

(

QPushButton

(

"Cancel"

)

)

main_layout.

addLayout

(

bottom_layout

)

window.

setLayout

(

main_layout

)

window.

show

(

)


Advanced layout features

1. Stretch Factor

Controls the relative size of widgets within a layout.

Example

layout

=

QHBoxLayout

(

)

layout.

addWidget

(

QPushButton

(

"Left"

)

,

stretch

=

1

)

 

Takes 1 share of the space

layout.

addWidget

(

QPushButton

(

"Center"

)

,

stretch

=

2

)

 

Takes 2 shares of the space

layout.

addWidget

(

QPushButton

(

"Right"

)

,

stretch

=

1

)

 

Takes 1 share of the space

2. Spacing control

Example

layout.

setSpacing

(

10

)

 

Spacing between widgets

layout.

setContentsMargins

(

20

,

20

,

20

,

20

)

 

Margins around the layout edges

3. Alignment

Example

button

=

QPushButton

(

"Button"

)

layout.

addWidget

(

button

,

alignment

=

Qt.

AlignRight

)

 

Align right


Best Practices

  1. Prefer layout managers: Avoid absolute positioning (setGeometry)
  2. Nest layouts sensibly: Combine different layouts to create complex interfaces
  3. Use stretch space: Make the interface look good at different sizes
  4. Stay consistent: Use the same layout approach for similar interface elements
  5. Test at different resolutions: Make sure the interface works properly on all screen sizes

By mastering PyQt's layout management system you can build professional, attractive and adaptable GUI application interfaces.