import sys from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QHBoxLayout class Example(QWidget): def __init__(self): super().__init__() self.initUI() def initUI(self): hbox = QHBoxLayout() b1 = QPushButton("Button 1") b2 = QPushButton("Button 2") b3 = QPushButton("Button 3") hbox.addWidget(b1) hbox.addWidget(b2) hbox.addWidget(b3) self.setLayout(hbox) self.setGeometry(300, 300, 300, 150) self.setWindowTitle("Horizontal Layout") self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())
import sys from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QHBoxLayout, QWidget class Example(QMainWindow): def __init__(self): super().__init__() self.initUI() def initUI(self): hbox = QHBoxLayout() b1 = QPushButton("Button 1") b2 = QPushButton("Button 2") b3 = QPushButton("Button 3") hbox.addWidget(b1) hbox.addWidget(b2) hbox.addWidget(b3) widget = QWidget() widget.setLayout(hbox) self.setCentralWidget(widget) self.setGeometry(300, 300, 300, 150) self.setWindowTitle("Horizontal Layout") self.show() if __name__ == '__main__': app = QApplication(sys.argv) ex = Example() sys.exit(app.exec_())This example demonstrates how to use QHBoxLayout to create three buttons arranged horizontally inside a QMainWindow. The QHBoxLayout is added to a QWidget which is then set as the central widget of the QMainWindow. Package Library: PyQt5.Qt.