所以我创建了一个有2页的堆叠式小部件。在第一页中,我有一个按钮“登录”。在第二页中,我有一个按钮“注销”
我想要的是,如果我单击“登录”按钮,它将在堆叠的小部件中使用“注销”按钮打开另一个页面/小部件。
首页索引= 0
第二页索引= 1
登录按钮对象名称= pushButton
注销按钮对象名称= LogoutBtn
这是我来自PyQt5的代码:
class Ui_StackedWidget(object):
def setupUi(self, StackedWidget):
StackedWidget.setObjectName("StackedWidget")
StackedWidget.resize(470, 406)
self.page1 = QtWidgets.QWidget()
self.page1.setObjectName("page1")
self.label = QtWidgets.QLabel(self.page1)
self.label.setGeometry(QtCore.QRect(150, 170, 35, 10))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.page1)
self.label_2.setGeometry(QtCore.QRect(150, 200, 35, 10))
self.label_2.setObjectName("label_2")
self.pushButton = QtWidgets.QPushButton(self.page1)
self.pushButton.setGeometry(QtCore.QRect(230, 230, 56, 17))
self.pushButton.setObjectName("pushButton")
self.lineEdit = QtWidgets.QLineEdit(self.page1)
self.lineEdit.setGeometry(QtCore.QRect(200, 170, 113, 16))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.page1)
self.lineEdit_2.setGeometry(QtCore.QRect(200, 200, 113, 16))
self.lineEdit_2.setObjectName("lineEdit_2")
self.label_3 = QtWidgets.QLabel(self.page1)
self.label_3.setGeometry(QtCore.QRect(230, 120, 35, 10))
self.label_3.setObjectName("label_3")
StackedWidget.addWidget(self.page1)
self.page = QtWidgets.QWidget()
self.page.setObjectName("page")
self.label_4 = QtWidgets.QLabel(self.page)
self.label_4.setGeometry(QtCore.QRect(180, 70, 101, 16))
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.page)
self.label_5.setGeometry(QtCore.QRect(190, 110, 91, 16))
self.label_5.setObjectName("label_5")
self.CreateBtn = QtWidgets.QPushButton(self.page)
self.CreateBtn.setGeometry(QtCore.QRect(200, 160, 56, 17))
self.CreateBtn.setObjectName("CreateBtn")
self.ShowBtn = QtWidgets.QPushButton(self.page)
self.ShowBtn.setGeometry(QtCore.QRect(200, 190, 56, 17))
self.ShowBtn.setObjectName("ShowBtn")
self.DeleteBtn = QtWidgets.QPushButton(self.page)
self.DeleteBtn.setGeometry(QtCore.QRect(200, 220, 56, 17))
self.DeleteBtn.setObjectName("DeleteBtn")
self.UpdateBtn = QtWidgets.QPushButton(self.page)
self.UpdateBtn.setGeometry(QtCore.QRect(200, 250, 56, 17))
self.UpdateBtn.setObjectName("UpdateBtn")
self.LogoutBtn = QtWidgets.QPushButton(self.page)
self.LogoutBtn.setGeometry(QtCore.QRect(320, 320, 56, 17))
self.LogoutBtn.setObjectName("LogoutBtn")
StackedWidget.addWidget(self.page)
self.page_2 = QtWidgets.QWidget()
self.page_2.setObjectName("page_2")
self.label_6 = QtWidgets.QLabel(self.page_2)
self.label_6.setGeometry(QtCore.QRect(150, 90, 141, 16))
self.label_6.setObjectName("label_6")
self.Logout1Btn = QtWidgets.QPushButton(self.page_2)
self.Logout1Btn.setGeometry(QtCore.QRect(330, 320, 56, 17))
self.Logout1Btn.setObjectName("Logout1Btn")
self.main1Btn = QtWidgets.QPushButton(self.page_2)
self.main1Btn.setGeometry(QtCore.QRect(260, 320, 56, 17))
self.main1Btn.setObjectName("main1Btn")
self.label_7 = QtWidgets.QLabel(self.page_2)
self.label_7.setGeometry(QtCore.QRect(200, 180, 35, 10))
self.label_7.setObjectName("label_7")
StackedWidget.addWidget(self.page_2)
self.retranslateUi(StackedWidget)
QtCore.QMetaObject.connectSlotsByName(StackedWidget)
然后我创建了一个函数切换(我在StackOverflow中看到了一个引用)_
def switching(self):
self.ui = Ui_StackedWidget()
self.ui.setupUi(self)
self.ui.LogoutBtn.clicked.connect(lambda : self.ui.StackedWidget.setCurrentIndex(0))
self.ui.pushButton.clicked.connect(lambda : self.ui.StackedWidget.setCurrentIndex(1))
但是它不会在Windows之间切换。
答案 0 :(得分:0)
根据您提供的代码,我推断出您使用StackedWidget作为模板,因此您使用的类将填充ui以从该类继承。
我在您的代码中看到的错误是self.ui.StackedWidget
,因为ui没有名为StackedWidget
的属性,因此您应该抛出异常。
考虑到上述情况,解决方案是:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_StackedWidget(object):
def setupUi(self, StackedWidget):
# ...
def retranslateUi(self, StackedWidget):
# ...
class StackedWidget(QtWidgets.QStackedWidget):
def __init__(self, parent=None):
super().__init__(parent)
self.ui = Ui_StackedWidget()
self.ui.setupUi(self)
self.ui.LogoutBtn.clicked.connect(lambda : self.setCurrentIndex(0))
self.ui.pushButton.clicked.connect(lambda : self.setCurrentIndex(1))
if __name__ == '__main__':
import sys
app = QtWidgets.QApplication(sys.argv)
w = StackedWidget()
w.show()
sys.exit(app.exec_())