在PyQt5中制作嵌套的StackedLayouts可以通过以下步骤实现:
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QStackedLayout, QPushButton
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
window.setLayout(layout)
# 子窗口1
sub_window1 = QWidget()
sub_layout1 = QHBoxLayout()
sub_button1 = QPushButton("Sub Window 1")
sub_layout1.addWidget(sub_button1)
sub_window1.setLayout(sub_layout1)
# 子窗口2
sub_window2 = QWidget()
sub_layout2 = QHBoxLayout()
sub_button2 = QPushButton("Sub Window 2")
sub_layout2.addWidget(sub_button2)
sub_window2.setLayout(sub_layout2)
stacked_layout = QStackedLayout()
stacked_layout.addWidget(sub_window1)
stacked_layout.addWidget(sub_window2)
button1 = QPushButton("Show Sub Window 1")
button2 = QPushButton("Show Sub Window 2")
def show_sub_window1():
stacked_layout.setCurrentWidget(sub_window1)
def show_sub_window2():
stacked_layout.setCurrentWidget(sub_window2)
button1.clicked.connect(show_sub_window1)
button2.clicked.connect(show_sub_window2)
layout.addWidget(button1)
layout.addWidget(button2)
layout.addLayout(stacked_layout)
window.show()
app.exec_()
通过以上步骤,你可以在PyQt5中制作嵌套的StackedLayouts。点击按钮可以切换显示不同的子窗口。这种布局方式适用于需要在同一区域显示不同内容的场景,例如选项卡式的界面或向导式的界面。
腾讯云相关产品和产品介绍链接地址:
领取专属 10元无门槛券
手把手带您无忧上云