python logo

pyqt5 layout


Python hosting: Host, run, and code Python in the cloud!

PyQt5 layouts are essential for designing interactive GUI applications. One such layout is the horizontal layout which allows you to dynamically add widgets such as buttons, text fields, images, and others, in a horizontal direction.

Using PyQt5’s horizontal layout, you can seamlessly align your widgets to create visually appealing interfaces for your applications. In this guide, we will delve into the steps to add buttons horizontally using the PyQt5 horizontal layout.

PyQt5 Horizontal Layout

Interested in mastering PyQt5? Check out this course:

Designing with the Horizontal Layout

Before we dive in, let’s take a comprehensive look at the code, followed by a detailed explanation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout, QGroupBox, QDialog, QVBoxLayout
from PyQt5.QtGui import QIcon
from PyQt5.QtCore import pyqtSlot

class App(QDialog):

def __init__(self):
super().__init__()
self.title = 'PyQt5 layout - pythonspot.com'
self.left = 10
self.top = 10
self.width = 320
self.height = 100
self.initUI()

def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)

self.createHorizontalLayout()

windowLayout = QVBoxLayout()
windowLayout.addWidget(self.horizontalGroupBox)
self.setLayout(windowLayout)

self.show()

def createHorizontalLayout(self):
self.horizontalGroupBox = QGroupBox("What is your favorite color?")
layout = QHBoxLayout()

buttonBlue = QPushButton('Blue', self)
buttonBlue.clicked.connect(self.on_click)
layout.addWidget(buttonBlue)

buttonRed = QPushButton('Red', self)
buttonRed.clicked.connect(self.on_click)
layout.addWidget(buttonRed)

buttonGreen = QPushButton('Green', self)
buttonGreen.clicked.connect(self.on_click)
layout.addWidget(buttonGreen)

self.horizontalGroupBox.setLayout(layout)

@pyqtSlot()
def on_click(self):
print('PyQt5 button click')

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

Decoding the Code

Let’s decipher how the horizontal layout works in PyQt5:

  1. Initialization: The method self.createHorizontalLayout() is called inside the initUI() method. This sets up the foundation for the horizontal layout.

  2. Box Creation: A box with a title and a horizontal layout is established with:

    1
    2
    self.horizontalGroupBox = QGroupBox("What is your favorite color?")
    layout = QHBoxLayout()
  3. Widget Addition: Widgets (for example, qpushbutton) are produced and incorporated into the layout sequentially:

    1
    2
    3
    buttonBlue = QPushButton('Blue', self)
    buttonBlue.clicked.connect(self.on_click)
    layout.addWidget(buttonBlue)
  4. Box Assignment: The title-box is designated to encompass the horizontal layout:

    1
    self.horizontalGroupBox.setLayout(layout)
  5. Window Integration: The box is integrated into the main window through:

    1
    2
    3
    windowLayout = QVBoxLayout()
    windowLayout.addWidget(self.horizontalGroupBox)
    self.setLayout(windowLayout)

If you’re intrigued by layouts, PyQt5 also offers the flexibility to create vertical layouts using a qvboxlayout.

For more hands-on examples and insights: Download PyQT5 Examples

Navigate to learn more: ← Previous | Next →






Leave a Reply: