我创建了一个登录页面,在我登录该窗口后,关闭并打开另一个窗口。但是,从第二个窗口中,我将无法通过按注销来打开登录窗口。这是我尝试过的:
应用了所有方法以不同的方式导入文件,但是没有用。
我尝试了此操作,但没有成功。
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_LoginWondow(object):
def LoginContent(self):
from LogoutContent import Ui_MainWindow
self.window = QtWidgets.QMainWindow()
self.ui = Ui_MainWindow()
self.ui.setupUi_LogoutContent(self.window)
self.window.show()
LoginWondow.close()
def setupUi_LoginContent(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(300, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(110, 120, 75, 23))
self.pushButton.setObjectName("pushButton")
# Button Clicked #
self.pushButton.clicked.connect(self.LoginContent)
######################
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Login"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
LoginWondow = QtWidgets.QMainWindow()
ui = Ui_LoginWondow()
ui.setupUi_LoginContent(LoginWondow)
LoginWondow.show()
sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def LogoutContent(self):
from LoginContent import Ui_LoginWondow
self.window = QtWidgets.QMainWindow()
self.ui = Ui_LoginWondow()
self.ui.setupUi_LoginContent(self.window)
self.window.show()
MainWindow.close()
def setupUi_LogoutContent(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(300, 300)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setGeometry(QtCore.QRect(100, 130, 75, 23))
self.pushButton.setObjectName("pushButton")
# Button Clicked #
self.pushButton.clicked.connect(self.LogoutContent)
######################
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "Logout"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
从LoginContent.py执行到LogoutContent.py,然后从LogoutContent.py返回到LoginContent.py,此错误消息显示:
Traceback (most recent call last):
File "LogoutContent.py", line 18, in LogoutContent
MainWindow.close()
NameError: name 'MainWindow' is not defined
这两个AdminMainWindow和LoginWindow已在最后定义。
在此先感谢