更新1: 在做出以下更正之后:
from PyQt4 import QtGui, QtCore
class LoginWidget(QtGui.QWidget):
success = QtCore.pyqtSignal() # Might be QtCore.pyqtSignal
def __init__(self, parent=None):
super(LoginWidget, self).__init__(parent)
self.Username = QtGui.QLineEdit(self)
self.Password = QtGui.QLineEdit(self)
self.buttonLogin = QtGui.QPushButton('Login', self)
self.buttonLogin.clicked.connect(self.handleLogin)
loginLayout = QtGui.QFormLayout()
loginLayout.addRow("Username", self.Username)
loginLayout.addRow("Password", self.Password)
layout = QtGui.QVBoxLayout(self)
layout.addLayout(loginLayout)
layout.addWidget(self.Username)
layout.addWidget(self.Password)
layout.addWidget(self.buttonLogin)
def handleLogin(self):
if (self.Username.text() == 'example' and
self.Password.text() == 'example'):
self.success.emit()
# OR you know that the main window is the parent of this class
# so you could call self.parent().P_3()
# Signals are better though
else:
QtGui.QMessageBox.warning(
self, 'Error', 'Incorrect Username/Password combination!')
class Page3(QtGui.QWidget):
def __init__(self, parent=None):
super(Page3, self).__init__(parent)
layout = QtGui.QHBoxLayout()
self.Hello = QtGui.QLabel('Hello')
layout.addWidget(self.Hello)
self.setLayout(layout)
class page1(QtGui.QWidget):
def __init__(self, parent=None):
super(page1, self).__init__(parent)
layout = QtGui.QHBoxLayout()
self.nextpage = QtGui.QPushButton('Page2')
layout.addWidget(self.nextpage)
self.setLayout(layout)
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
Page1 = page1(self)
Page1.nextpage.clicked.connect(self.P_2)
self.central_widget.addWidget(Page1)
def P_2(self):
page2 = LoginWidget(self)
# Connect your signal to a method. When success is emitted it will call P_3()
page2.success.connect(self.P_3) # Note: P_3 does not have "()" with it
self.central_widget.addWidget(page2)
self.central_widget.setCurrentWidget(page2)
def P_3(self):
print("yay")
page3 = Page3(self)
# self.central_widget.addWidget(Page3) # you are calling the class (lowercase)
# self.central_widget.setCurrentWidget(Page3) # calling the class (lowercase)
self.central_widget.addWidget(Page3)
self.central_widget.setCurrentWidget(Page3)
if __name__ == '__main__':
User = ''
app = QtGui.QApplication([])
window = MainWindow()
window.showFullScreen()
app.exec_()
我收到此错误:
追踪(最近一次通话): P_3中的文件“C:\ Users \ Hamzah \ My Documents \ Work \ A-Level \ USB Stuff \ Pie Chart 2.py”,第66行 self.central_widget.addWidget(第3页) TypeError:QStackedWidget.addWidget(QWidget):参数1具有意外类型'PyQt4.QtCore.pyqtWrapperType
问题:
下面的代码显示了在第1页,登录页面(第2页)和第3页之间切换布局的示例。但是我似乎无法将布局从第2页切换到第3页。我可以将布局从第1页切换到第2页虽然?!顺便说一句,在运行程序时(如果你运行它),登录详细信息是'example'和'example'或者查看代码以获取详细信息:
from PyQt4 import QtCore, QtGui
class MainWindow(QtGui.QMainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.central_widget = QtGui.QStackedWidget()
self.setCentralWidget(self.central_widget)
Page1 = page1(self)
Page1.nextpage.clicked.connect(self.P_2)
self.central_widget.addWidget(Page1)
def P_2(self):
page2 = LoginWidget(self)
self.central_widget.addWidget(page2)
self.central_widget.setCurrentWidget(page2)
def P_3(self):
print("Why won't the page open!!!???")
page3 = Page3(self)
self.central_widget.addWidget(Page3)
self.central_widget.setCurrentWidget(Page3)
class LoginWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(LoginWidget, self).__init__(parent)
self.Username = QtGui.QLineEdit(self)
self.Password = QtGui.QLineEdit(self)
self.buttonLogin = QtGui.QPushButton('Login', self)
self.buttonLogin.clicked.connect(self.handleLogin)
loginLayout = QtGui.QFormLayout()
loginLayout.addRow("Username", self.Username)
loginLayout.addRow("Password", self.Password)
layout = QtGui.QVBoxLayout(self)
layout.addLayout(loginLayout)
layout.addWidget(self.Username)
layout.addWidget(self.Password)
layout.addWidget(self.buttonLogin)
def handleLogin(self):
if (self.Username.text() == 'example' and
self.Password.text() == 'example'):
MainWindow().P_3()
else:
QtGui.QMessageBox.warning(
self, 'Error', 'Incorrect Username/Password combination!')
class page1(QtGui.QWidget):
def __init__(self, parent=None):
super(page1, self).__init__(parent)
layout = QtGui.QHBoxLayout()
self.nextpage = QtGui.QPushButton('Page2')
layout.addWidget(self.nextpage)
self.setLayout(layout)
class Page3(QtGui.QWidget):
def __init__(self, parent=None):
super(Page3, self).__init__(parent)
layout = QtGui.QHBoxLayout()
self.Hello = QtGui.QLabel('Hello')
layout.addWidget(self.Hello)
self.setLayout(layout)
if __name__ == '__main__':
User = ''
app = QtGui.QApplication([])
window = MainWindow()
window.showFullScreen()
app.exec_()
答案 0 :(得分:3)
问题出在您的LoginWidget中。 handleLogin正在创建一个新的MainWinow并尝试调用第3页。
您不想创建新的主窗口,并且您不想要登录所需的主窗口实例。
我建议使用信号
class LoginWidget(QtGui.QWidget):
success = QtCore.Signal() # Might be QtCore.pyqtSignal
...
def handleLogin(self):
if (self.Username.text() == 'example' and
self.Password.text() == 'example'):
self.success.emit()
# OR you know that the main window is the parent of this class
# so you could call self.parent().P_3()
# Signals are better though
else:
QtGui.QMessageBox.warning(
self, 'Error', 'Incorrect Username/Password combination!')
class MainWindow(QtGui.QMainWindow):
def P_2(self):
page2 = LoginWidget(self)
# Connect your signal to a method. When success is emitted it will call P_3()
page2.success.connect(self.P_3) # Note: P_3 does not have "()" with it
self.central_widget.addWidget(page2)
self.central_widget.setCurrentWidget(page2)
如果需要涉及信号的参数,请使用lambda函数。
page2.success.connect(lambda arg="": method(arg))
另请注意P_3中的错误
def P_3(self):
page3 = Page3(self)
# self.central_widget.addWidget(Page3) # you are calling the class (lowercase)
# self.central_widget.setCurrentWidget(Page3) # calling the class (lowercase)
self.central_widget.addWidget(page3)
self.central_widget.setCurrentWidget(page3)