目标:
我有一个初始窗口,有 4 个由按钮表示的选项。当用户点击按钮时,窗口内容应该被清除(例如,所有按钮都应该消失),并被新内容替换(可能是另一个按钮,一个文本字段,......)
当前代码:
import sys
import PyQt5
from PyQt5 import QtWidgets, QtGui
from PyQt5.QtWidgets import QLineEdit, QApplication, QWidget, QStackedWidget
from PyQt5.QtGui import QImage, QIcon, QPixmap, QPalette, QFont
class QWizard(QtWidgets.QWizard):
def __init__(self):
super().__init__()
self.init_window()
self.initial_options()
def init_window(self):
self.app = QtWidgets.QApplication(sys.argv)
self.setWizardStyle(QtWidgets.QWizard.ModernStyle)
self.setWindowTitle("TILE")
self.setMinimumSize(800,600)
self.page1 = QtWidgets.QWizardPage()
self.font = QFont()
self.font.setWeight(QFont.Bold)
self.page1.setTitle('INTRO TITLE')
self.page1.setSubTitle('SUBTITLE')
def initial_options(self):
b1 = QtWidgets.QPushButton("BUTTON 1")
b1.setFont(self.font)
b1.setMinimumSize(100,100)
b1.clicked.connect(self.b1_clicked)
b2 = QtWidgets.QPushButton("BUTTON 2")
b2.setFont(self.font)
b2.setMinimumSize(100,100)
b3 = QtWidgets.QPushButton("BUTTON 3")
b3.setFont(self.font)
b3.setMinimumSize(100,100)
b4 = QtWidgets.QPushButton("BUTTON 4")
b4.setFont(self.font)
b4.setMinimumSize(100,100)
hLayout1 = QtWidgets.QFormLayout(self.page1)
hLayout1.addRow(b1)
hLayout1.addRow(b2)
hLayout1.addRow(b3)
hLayout1.addRow(b4)
self.addPage(self.page1)
self.show()
def b1_clicked(self):
print("CLICKED BUTTON 1 - CREATE NEW/FRESH SWINDOW")
self.b1_click = QtWidgets.QWizardPage()
font = QFont()
font.setWeight(QFont.Bold)
self.b1_click.setTitle('TITLE OF NEW WINOW')
self.b1_click.setSubTitle('DO SOMETHING IN THIS WINDOW')
self.addPage(self.b1_click)
self.show
App = QApplication(sys.argv)
window = QWizard()
sys.exit(App.exec())
输出:
单击按钮 1 时,所有按钮都应消失,标题应更改,...一切都应该是新的。
我想到了解决此问题的 2 个选项:
- 用新窗口替换初始窗口(销毁它,隐藏它,...)。
- 为每个按钮/标题创建不同的小部件,并在单击按钮时将其隐藏。想到 QStackedWidgets / QStackedLayout 但我在代码中实现它时遇到了麻烦