We will examine the topic of groupbox, an important topic from our GUI articles..
Why am I saying important? because it will benefit us a lot in form design..
It will help us better use the regions of the form.
While using this widget, we will also use horizontal and vertical layouts.
I set the form main layout as horizontal layout, QHBoxLayout.
I will create 2 groupboxes. there will be vertical buttons in the groupbox on the left and vertical buttons in the groupbox on the right
Let the form be horizontal in general
h_general_form_layout = QHBoxLayout()
I’m calling methods that make groupbox
self.createGroupBox1() self.createGroupBox2()
I add 2 groupboxes to my general layout
h_general_form_layout.addWidget(self.groupbox_1) h_general_form_layout.addWidget(self.groupbox_2)
I am setting the general layout
self.setLayout(h_general_form_layout)
my groupbox methods
def createGroupBox1(self): self.groupbox_1 = QGroupBox("Group 1") h_layout = QHBoxLayout() button1 = QPushButton("button -1",self) button2 = QPushButton("button -2",self) button3 = QPushButton("button -3",self) h_layout.addWidget(button1) h_layout.addWidget(button2) h_layout.addWidget(button3) self.groupbox_1.setLayout(h_layout) def createGroupBox2(self): self.groupbox_2 = QGroupBox("Group 2") v_layout = QVBoxLayout() button1 = QPushButton("button -4", self) button2 = QPushButton("button -5", self) button3 = QPushButton("button -6", self) v_layout.addWidget(button1) v_layout.addWidget(button2) v_layout.addWidget(button3) self.groupbox_2.setLayout(v_layout)
Result :
Full Source Code :
from PyQt5 import QtWidgets from PyQt5.QtWidgets import * import sys W_WIDTH = 500 W_HEIGHT = 250 W_X = 725 W_Y = 350 class CreateButton: def __init__(self, window, text, width, height, x, y): self.button = QtWidgets.QPushButton(window) self.button.setText(text) self.button.resize(width, height) self.button.move(x, y) def get_instance(self): return self.button class MainFrame(QWidget): def __init__(self): super(MainFrame, self).__init__() self.init() self.show() def init(self): self.resize(W_WIDTH, W_HEIGHT) self.move(W_X, W_Y) self.setWindowTitle("www.langpy.com | Python GUI Tutorial") h_general_form_layout = QHBoxLayout() self.createGroupBox1() self.createGroupBox2() h_general_form_layout.addWidget(self.groupbox_1) h_general_form_layout.addWidget(self.groupbox_2) self.setLayout(h_general_form_layout) def createGroupBox1(self): self.groupbox_1 = QGroupBox("Group 1") h_layout = QHBoxLayout() button1 = QPushButton("button -1",self) button2 = QPushButton("button -2",self) button3 = QPushButton("button -3",self) h_layout.addWidget(button1) h_layout.addWidget(button2) h_layout.addWidget(button3) self.groupbox_1.setLayout(h_layout) def createGroupBox2(self): self.groupbox_2 = QGroupBox("Group 2") v_layout = QVBoxLayout() button1 = QPushButton("button -4", self) button2 = QPushButton("button -5", self) button3 = QPushButton("button -6", self) v_layout.addWidget(button1) v_layout.addWidget(button2) v_layout.addWidget(button3) self.groupbox_2.setLayout(v_layout) app = QApplication(sys.argv) win = MainFrame() sys.exit(app.exec_())