在python中尝试关闭和打开当前对话框窗口时出错

在python中尝试关闭和打开当前对话框窗口时出错,python,Python,SignIn.py from PyQt5 import QtCore, QtGui, QtWidgets from HomePage import Ui_HomePage from PyQt5.QtWidgets import QMessageBox import cx_Oracle class Ui_SignIn(object): def openConnection(self): con=cx_Oracle.connect('lab/oracle@localhost

SignIn.py

from PyQt5 import QtCore, QtGui, QtWidgets
from HomePage import Ui_HomePage
from PyQt5.QtWidgets import QMessageBox
import cx_Oracle

class Ui_SignIn(object):
    def openConnection(self):
        con=cx_Oracle.connect('lab/oracle@localhost:1521/xe')

    def showMessageBox(self,title,message):
        #QMessageBox.about(self, "Title", "Message")
        msgBox = QtWidgets.QMessageBox()
        msgBox.setIcon(QtWidgets.QMessageBox.Warning)
        msgBox.setWindowTitle(title)
        msgBox.setText(message)
        msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
        msgBox.exec_()

    def loginCheck(self):
        username = self.ledt_username.text()
        password = self.ledt_password.text()
        #print(username)
        #print(password)
        con=cx_Oracle.connect('lab/oracle@localhost:1521/xe')
        cur=con.cursor()
        cur.execute('select * from user_tbl where username=:1 and password=:2', (username, password))
        #cur.execute('select * from user_tbl')
        cur.fetchall()
        #n=0
        #for result in cur:
            #n=n+1
        #m=cur.rowcount
        #print(n)
        #print(m)
        if(cur.rowcount > 0):
            print("User Found ! ")
            Dialog.hide()
            self.welcomeWindowShow()
        else:
            print("User Not Found !")
            #self.showMessageBox()
            self.showMessageBox('Warning','Invalid Username And Password')
            #self.welcomeWindowShow()
        con.close()

    def welcomeWindowShow(self):
        self.window=QtWidgets.QDialog()
        self.ui=Ui_HomePage()
        self.ui.setupUi(self.window)
        self.window.show()

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(690, 450)
        Dialog.setBaseSize(QtCore.QSize(0, 0))
        self.lbl_username = QtWidgets.QLabel(Dialog)
        self.lbl_username.setGeometry(QtCore.QRect(250, 100, 61, 21))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.lbl_username.setFont(font)
        self.lbl_username.setLayoutDirection(QtCore.Qt.RightToLeft)
        self.lbl_username.setAutoFillBackground(False)
        self.lbl_username.setObjectName("lbl_username")
        self.ledt_username = QtWidgets.QLineEdit(Dialog)
        self.ledt_username.setGeometry(QtCore.QRect(330, 100, 141, 21))
        self.ledt_username.setObjectName("ledt_username")
        self.lbl_password = QtWidgets.QLabel(Dialog)
        self.lbl_password.setGeometry(QtCore.QRect(250, 150, 71, 21))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.lbl_password.setFont(font)
        self.lbl_password.setAutoFillBackground(False)
        self.lbl_password.setObjectName("lbl_password")
        self.ledt_password = QtWidgets.QLineEdit(Dialog)
        self.ledt_password.setGeometry(QtCore.QRect(330, 150, 141, 20))
        self.ledt_password.setObjectName("ledt_password")
        self.pbtn_signin = QtWidgets.QPushButton(Dialog)
        self.pbtn_signin.setGeometry(QtCore.QRect(270, 200, 75, 23))
        self.pbtn_signin.setObjectName("pbtn_signin")
        ############## logincheck calling ######
        self.pbtn_signin.clicked.connect(self.loginCheck)
        ###################################################
        self.pbtn_signup = QtWidgets.QPushButton(Dialog)
        self.pbtn_signup.setGeometry(QtCore.QRect(380, 200, 75, 23))
        self.pbtn_signup.setObjectName("pbtn_signup")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.lbl_username.setText(_translate("Dialog", "Username"))
        self.lbl_password.setText(_translate("Dialog", "Password"))
        self.pbtn_signin.setText(_translate("Dialog", "Sign In"))
        self.pbtn_signup.setText(_translate("Dialog", "Sign Up"))

   # from HomePage import Ui_HomePage


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_SignIn()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
#from SignIn import Ui_SignIn

class Ui_HomePage(object):
    def importSignIn(self):
        from SignIn import Ui_SignIn

    def signInWindowShow(self):
        Dialog.hide()
        from SignIn import Ui_SignIn
        self.window=QtWidgets.QDialog()
        self.ui=Ui_SignIn()
        self.ui.setupUi(self.window)
        self.window.show()
        #self.Dialog.hide()

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(696, 395)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(210, 50, 161, 41))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(300, 220, 75, 23))
        self.pushButton.setObjectName("pushButton")
        ############## logincheck calling ######
        self.pushButton.clicked.connect(self.signInWindowShow)
        ###################################################
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Welcome"))
        self.pushButton.setText(_translate("Dialog", "Logout"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_HomePage()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
HomePage.py

from PyQt5 import QtCore, QtGui, QtWidgets
from HomePage import Ui_HomePage
from PyQt5.QtWidgets import QMessageBox
import cx_Oracle

class Ui_SignIn(object):
    def openConnection(self):
        con=cx_Oracle.connect('lab/oracle@localhost:1521/xe')

    def showMessageBox(self,title,message):
        #QMessageBox.about(self, "Title", "Message")
        msgBox = QtWidgets.QMessageBox()
        msgBox.setIcon(QtWidgets.QMessageBox.Warning)
        msgBox.setWindowTitle(title)
        msgBox.setText(message)
        msgBox.setStandardButtons(QtWidgets.QMessageBox.Ok)
        msgBox.exec_()

    def loginCheck(self):
        username = self.ledt_username.text()
        password = self.ledt_password.text()
        #print(username)
        #print(password)
        con=cx_Oracle.connect('lab/oracle@localhost:1521/xe')
        cur=con.cursor()
        cur.execute('select * from user_tbl where username=:1 and password=:2', (username, password))
        #cur.execute('select * from user_tbl')
        cur.fetchall()
        #n=0
        #for result in cur:
            #n=n+1
        #m=cur.rowcount
        #print(n)
        #print(m)
        if(cur.rowcount > 0):
            print("User Found ! ")
            Dialog.hide()
            self.welcomeWindowShow()
        else:
            print("User Not Found !")
            #self.showMessageBox()
            self.showMessageBox('Warning','Invalid Username And Password')
            #self.welcomeWindowShow()
        con.close()

    def welcomeWindowShow(self):
        self.window=QtWidgets.QDialog()
        self.ui=Ui_HomePage()
        self.ui.setupUi(self.window)
        self.window.show()

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(690, 450)
        Dialog.setBaseSize(QtCore.QSize(0, 0))
        self.lbl_username = QtWidgets.QLabel(Dialog)
        self.lbl_username.setGeometry(QtCore.QRect(250, 100, 61, 21))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.lbl_username.setFont(font)
        self.lbl_username.setLayoutDirection(QtCore.Qt.RightToLeft)
        self.lbl_username.setAutoFillBackground(False)
        self.lbl_username.setObjectName("lbl_username")
        self.ledt_username = QtWidgets.QLineEdit(Dialog)
        self.ledt_username.setGeometry(QtCore.QRect(330, 100, 141, 21))
        self.ledt_username.setObjectName("ledt_username")
        self.lbl_password = QtWidgets.QLabel(Dialog)
        self.lbl_password.setGeometry(QtCore.QRect(250, 150, 71, 21))
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        self.lbl_password.setFont(font)
        self.lbl_password.setAutoFillBackground(False)
        self.lbl_password.setObjectName("lbl_password")
        self.ledt_password = QtWidgets.QLineEdit(Dialog)
        self.ledt_password.setGeometry(QtCore.QRect(330, 150, 141, 20))
        self.ledt_password.setObjectName("ledt_password")
        self.pbtn_signin = QtWidgets.QPushButton(Dialog)
        self.pbtn_signin.setGeometry(QtCore.QRect(270, 200, 75, 23))
        self.pbtn_signin.setObjectName("pbtn_signin")
        ############## logincheck calling ######
        self.pbtn_signin.clicked.connect(self.loginCheck)
        ###################################################
        self.pbtn_signup = QtWidgets.QPushButton(Dialog)
        self.pbtn_signup.setGeometry(QtCore.QRect(380, 200, 75, 23))
        self.pbtn_signup.setObjectName("pbtn_signup")

        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.lbl_username.setText(_translate("Dialog", "Username"))
        self.lbl_password.setText(_translate("Dialog", "Password"))
        self.pbtn_signin.setText(_translate("Dialog", "Sign In"))
        self.pbtn_signup.setText(_translate("Dialog", "Sign Up"))

   # from HomePage import Ui_HomePage


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_SignIn()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())
from PyQt5 import QtCore, QtGui, QtWidgets
#from SignIn import Ui_SignIn

class Ui_HomePage(object):
    def importSignIn(self):
        from SignIn import Ui_SignIn

    def signInWindowShow(self):
        Dialog.hide()
        from SignIn import Ui_SignIn
        self.window=QtWidgets.QDialog()
        self.ui=Ui_SignIn()
        self.ui.setupUi(self.window)
        self.window.show()
        #self.Dialog.hide()

    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(696, 395)
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(210, 50, 161, 41))
        font = QtGui.QFont()
        font.setPointSize(16)
        font.setBold(True)
        font.setWeight(75)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.pushButton = QtWidgets.QPushButton(Dialog)
        self.pushButton.setGeometry(QtCore.QRect(300, 220, 75, 23))
        self.pushButton.setObjectName("pushButton")
        ############## logincheck calling ######
        self.pushButton.clicked.connect(self.signInWindowShow)
        ###################################################
        self.retranslateUi(Dialog)
        QtCore.QMetaObject.connectSlotsByName(Dialog)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "Welcome"))
        self.pushButton.setText(_translate("Dialog", "Logout"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()
    ui = Ui_HomePage()
    ui.setupUi(Dialog)
    Dialog.show()
    sys.exit(app.exec_())

在python中尝试关闭和打开当前对话框窗口时出错 我需要在循环中打开和关闭两个窗口

还附加了我得到的错误


错误的原因是
对话框
登录检查
方法的范围内不可用,需要在以下行中作为参数显式传递:

self.pbtn_signin.clicked.connect(self.loginCheck)
下面是答案,如何做到这一点

其中一个选项是从
functools
导入
partial

from functools import partial
并使用此功能将
对话框
传递到
登录检查

self.pbtn_signin.clicked.connect(partial(self.loginCheck, Dialog))

已删除不需要的文本并为您内联图像,但您最好在问题中添加错误消息的文本。
对话框
未在
登录检查中定义,是吗?