我无法通过取消按钮关闭简单登录应用的对话框。
我绕圈试图解决这个问题,但我一直都不知道"属性错误"。
我使用pyuic5为ui生成ptyhon代码,然后将该部分复制到main.py的末尾
这段代码看起来与我在网上看到的其他例子不同
有什么建议吗?
Login_Dialog.py
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_loginDialog(object):
def setupUi(self, loginDialog):
loginDialog.setObjectName("loginDialog")
loginDialog.resize(400, 300)
self.newuser_pushButton = QtWidgets.QPushButton(loginDialog)
self.newuser_pushButton.setGeometry(QtCore.QRect(50, 230, 75, 31))
self.newuser_pushButton.setObjectName("newuser_pushButton")
self.login_pushButton = QtWidgets.QPushButton(loginDialog)
self.login_pushButton.setGeometry(QtCore.QRect(160, 230, 75, 31))
self.login_pushButton.setObjectName("login_pushButton")
self.cancel_pushButton = QtWidgets.QPushButton(loginDialog)
self.cancel_pushButton.setGeometry(QtCore.QRect(270, 230, 75, 31))
self.cancel_pushButton.setObjectName("cancel_pushButton")
self.username_LineEdit = QtWidgets.QLineEdit(loginDialog)
self.username_LineEdit.setGeometry(QtCore.QRect(140, 60, 201, 31))
self.username_LineEdit.setObjectName("username_LineEdit")
self.password_LineEdit = QtWidgets.QLineEdit(loginDialog)
self.password_LineEdit.setGeometry(QtCore.QRect(140, 110, 201, 31))
self.password_LineEdit.setEchoMode(QtWidgets.QLineEdit.Password)
self.password_LineEdit.setObjectName("password_LineEdit")
self.label = QtWidgets.QLabel(loginDialog)
self.label.setGeometry(QtCore.QRect(30, 70, 91, 16))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(loginDialog)
self.label_2.setGeometry(QtCore.QRect(60, 120, 47, 13))
self.label_2.setObjectName("label_2")
self.retranslateUi(loginDialog)
QtCore.QMetaObject.connectSlotsByName(loginDialog)
def retranslateUi(self, loginDialog):
_translate = QtCore.QCoreApplication.translate
loginDialog.setWindowTitle(_translate("loginDialog", "Login"))
self.newuser_pushButton.setText(_translate("loginDialog", "New User"))
self.login_pushButton.setText(_translate("loginDialog", "Login"))
self.cancel_pushButton.setText(_translate("loginDialog", "Cancel"))
self.label.setText(_translate("loginDialog", "Username (Email)"))
self.label_2.setText(_translate("loginDialog", "Password"))
main.py
import sys
import Login_Dialog
from PyQt5 import QtCore, QtGui, QtWidgets
from Login_Dialog import Ui_loginDialog
class ui(Login_Dialog):
def __init__(self):
super(ui, self).__init__()
self.cancel_pushButton.clicked.connect(self.reject)
def reject(self):
self.close()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
loginDialog = QtWidgets.QDialog()
ui = Ui_loginDialog()
ui.setupUi(loginDialog)
loginDialog.show()
sys.exit(app.exec_())
答案 0 :(得分:0)
您的代码有以下错误:
Login_Dialog是指.py文件,它不是一个类,所以你不能继承它。
Qt Designer仅提供用于填充窗口小部件的设计类,它不是窗口小部件。 PyQt建议创建一个新的小部件并使用Qt Designer提供的类来填充它。
拒绝是一种QDialog
方法,因此您可以直接连接它,关闭窗口而不使用close()
综上所述,您的代码应如下所示:
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
from Login_Dialog import Ui_loginDialog
class ui(QtWidgets.QDialog, Ui_loginDialog):
def __init__(self):
super(ui, self).__init__()
self.setupUi(self)
self.cancel_pushButton.clicked.connect(self.reject)
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
w = ui()
w.show()
sys.exit(app.exec_())