PyQt QTabWidget
While using the GUI program, you can see a window with a Tab as shown above.
These tabs can be useful because the components in the program do not take up a large area and can be categorized according to categories.
In a simple example, let's create a widget with two tabs.
Related course: Create Desktop Apps with Python PyQt5
QTabWidget
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QTabWidget, QVBoxLayout
class MyApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
tab1 = QWidget()
tab2 = QWidget()
tabs = QTabWidget()
tabs.addTab(tab1, 'Tab1')
tabs.addTab(tab2, 'Tab2')
vbox = QVBoxLayout()
vbox.addWidget(tabs)
self.setLayout(vbox)
self.setWindowTitle('QTabWidget')
self.setGeometry(300, 300, 300, 200)
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = MyApp()
sys.exit(app.exec_())
A small widget with two tabs is created.
Description
tab1 = QWidget()
tab2 = QWidget()
You have created two widgets to be placed on each tab.
tabs = QTabWidget()
tabs.addTab(tab1, 'Tab1')
tabs.addTab(tab2, 'Tab2')
Use QTabWidget() to create tabs, and add Tab1 and Tab2 to tabs using addTab().
vbox = QVBoxLayout()
vbox.addWidget(tabs)
self.setLayout(vbox)
Create one vertical box layout and put tabs.
Then set the vertical vbox to the widget's layout.
Related course: Create Desktop Apps with Python PyQt5