Tuesday, March 29, 2022

Tabbed Container with PyQt6

 This demo program show how to create a tab widget, place it on a specific location in a window, add widgets to each tab among others.

The output:



The code:


 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
from PyQt6.QtCore import *
from PyQt6.QtWidgets import *
import sys

class Window(QWidget):
    def __init__(self):
        QWidget.__init__(self)
        self.tab1 = QWidget()
        self.tab2 = QWidget()

        label1 = QLabel("Widget in Tab 1.")
        label2 = QLabel("Widget in Tab 2.")
        label3 = QLabel("Test Widget in Tab 1.")
        label4 = QLabel("Test Widget in Tab 2.")
        self.tabwidget = QTabWidget(self)
        self.tabwidget.addTab(self.tab1,"Tab 1")
        self.tabwidget.addTab(self.tab2,"Tab 2")
        layout = QFormLayout()
        layout.addRow(label1)
        layout.addRow(label3)
        self.tab1.setLayout(layout)
        layout1 = QFormLayout()
        layout1.addRow(label2)
        layout1.addRow(label4)
        self.tab2.setLayout(layout1)

        self.tabwidget.setGeometry(25, 25, 350, 260)
        self.setGeometry(300, 300, 490,300)

app = QApplication(sys.argv)
screen = Window()
screen.show()
sys.exit(app.exec())

No comments:

Post a Comment