python - 尝试在 python 中关闭和打开当前对话框窗口时出错
问题描述
登录.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_())
主页.py
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 中关闭和打开当前对话框窗口时出错我需要循环打开和关闭两个窗口。
还附上了我得到的错误。
解决方案
错误的原因是Dialog
在方法范围内不可用,loginCheck
需要在以下行中作为参数显式传递:
self.pbtn_signin.clicked.connect(self.loginCheck)
这是带有答案的问题,如何做到这一点。
选项之一是partial
从以下位置导入functools
:
from functools import partial
并使用此函数传递Dialog
给loginCheck
:
self.pbtn_signin.clicked.connect(partial(self.loginCheck, Dialog))
推荐阅读
- python - django.db.utils.IntegrityError:唯一约束失败:new__vacancies_company.owner_id 错误
- java - 当我尝试编辑一个字段时,另一个字段在 Firestore Database Android 中设置为 NULL
- javascript - 使用javaScript从数组中的元素中删除逗号
- database - phpMyAdmin - 使两个表同步
- python - if 语句块返回的最佳模式是什么?
- javascript - 仅在一页上响应上下文更新
- swift - 如何轻松转换字典
. 字典的子序列 - python - 使用 Python 从列中删除特定的最终值
- php - 如何使用 Google Drive API 将文件共享到特定文件夹?
- asp.net-core - 在仅限内部的 Blazor 应用程序中使用 Windows 身份验证是否安全?