[PyQt5]高级控件7 - 堆叠布局QStackedLayout

wuchangjian2021-11-09 06:33:55编程学习

文章目录

  • 效果图
  • 完整代码

效果图

stackedlayout示例图

完整代码

import sys
from PyQt5.QtWidgets import QLabel,QStackedLayout,QWidget,QApplication,QMainWindow
from PyQt5.QtCore import *

class QStackedLayoutDemo(QMainWindow):
    def __init__(self):
        super(QStackedLayoutDemo, self).__init__()

        #设置窗口大小
        self.resize(400, 150)
        self.setWindowTitle("QStackedLayoutDemo")

        label1 = QLabel("标签 ---> 111111111111")
        label2 = QLabel("标签 ---> 222222222222")
        label3 = QLabel("标签 ---> 333333333333")

        stackedLayout = QStackedLayout()
        stackedLayout.addWidget(label1)
        stackedLayout.addWidget(label2)
        stackedLayout.addWidget(label3)
        stackedLayout.setCurrentIndex(2)
        timer = QTimer(self)
        timer.timeout.connect(lambda: stackedLayout.setCurrentIndex((stackedLayout.currentIndex() + 1) % stackedLayout.count()))
        timer.start(1000)
        stackedLayout.currentChanged.connect(lambda val: print(val))

        self.setLayout(stackedLayout)
        mainFrame = QWidget()
        mainFrame.setLayout(stackedLayout)
        self.setCentralWidget(mainFrame)

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

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。