QTabWidget 사용법
반응형
import sys, os
from PyQt5.QtWidgets import QTabWidget, QLabel, QWidget, QVBoxLayout, QApplication, QMessageBox


class BaseWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.initUI()

    def initUI(self):

        self.tabs = QTabWidget()

        self.tabs.blockSignals(True) #just for not showing the initial message
        self.tabs.currentChanged.connect(self.onChange) #changed!

        self.lbl1 = QLabel("Label 1")
        self.lbl2 = QLabel("Label 2")

        centralLayout = QVBoxLayout()
        centralLayout.addWidget(self.tabs, 1)

        self.tabs.addTab(self.lbl1, "tab 1")
        self.tabs.addTab(self.lbl2, "tab 2")

        vbox = QVBoxLayout()
        vbox.addStretch(3)
        vbox.addWidget(self.tabs)
        vbox.addStretch(1)

        self.setLayout(vbox)

        self.tabs.blockSignals(False) #now listen the currentChanged signal

        self.setWindowTitle('QTabWidget')
        self.setGeometry(0, 0, 500, 500)
        self.show()

    #@pyqtSlot()
    def onChange(self,i): #changed!
        QMessageBox.information(self,
                  "Tab Index Changed!",
                  "Current Tab Index: %d" % i ) #changed!

if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = BaseWindow()
    window.show()
    sys.exit(app.exec_())

 

 

 

PyQT5 : https://doc.qt.io/qtforpython/PySide6/QtWidgets/QTabWidget.html?highlight=qtabwidget

반응형