我的退出按钮btnCancel不工作,尽管把正确的代码?

2024-10-03 11:14:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我的代码的问题是,它不允许我关闭登录表单界面,我对使用PyQt4还很陌生,如果有人帮助我在单击“btnCancel”后退出表单,我会很高兴的。

这是我的代码:

from PyQt4 import QtCore, QtGui
import sys
import os

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)


class Ui_frmLogin(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)
        self.setWindowIcon(QtGui.QIcon('pythonlogo.png'))

    def setupUi(self, frmLogin):
        frmLogin.setObjectName(_fromUtf8("frmLogin"))
        frmLogin.resize(394, 138)
        frmLogin.setMinimumSize(QtCore.QSize(394, 0))
        frmLogin.setStyleSheet(_fromUtf8("QDialog{\n"
"background-color: rgb(211, 223, 255);\n"
"}"))
        self.verticalLayout = QtGui.QVBoxLayout(frmLogin)
        self.verticalLayout.setObjectName(_fromUtf8("verticalLayout"))
        self.label = QtGui.QLabel(frmLogin)
        font = QtGui.QFont()
        font.setFamily(_fromUtf8("DINPro-Black"))
        font.setPointSize(16)
        self.label.setFont(font)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName(_fromUtf8("label"))
        self.verticalLayout.addWidget(self.label)
        self.horizontalLayout_3 = QtGui.QHBoxLayout()
        self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3"))
        self.lblUsername = QtGui.QLabel(frmLogin)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lblUsername.setFont(font)
        self.lblUsername.setStyleSheet(_fromUtf8("QLabel {\n"
"    color: rgb(0, 0, 0)\n"
"}"))
        self.lblUsername.setAlignment(QtCore.Qt.AlignCenter)
        self.lblUsername.setObjectName(_fromUtf8("lblUsername"))
        self.horizontalLayout_3.addWidget(self.lblUsername)
        self.leUsername = QtGui.QLineEdit(frmLogin)
        self.leUsername.setObjectName(_fromUtf8("leUsername"))
        self.horizontalLayout_3.addWidget(self.leUsername)
        self.verticalLayout.addLayout(self.horizontalLayout_3)
        self.horizontalLayout_4 = QtGui.QHBoxLayout()
        self.horizontalLayout_4.setSpacing(4)
        self.horizontalLayout_4.setObjectName(_fromUtf8("horizontalLayout_4"))
        self.lblPassword = QtGui.QLabel(frmLogin)
        font = QtGui.QFont()
        font.setPointSize(10)
        self.lblPassword.setFont(font)
        self.lblPassword.setStyleSheet(_fromUtf8("QLabel {\n"
"    color: rgb(0, 0, 0)\n"
"}"))
        self.lblPassword.setAlignment(QtCore.Qt.AlignCenter)
        self.lblPassword.setObjectName(_fromUtf8("lblPassword"))
        self.horizontalLayout_4.addWidget(self.lblPassword)
        self.lePassword = QtGui.QLineEdit(frmLogin)
        self.lePassword.setObjectName(_fromUtf8("lePassword"))
        self.horizontalLayout_4.addWidget(self.lePassword)
        self.verticalLayout.addLayout(self.horizontalLayout_4)
        self.horizontalLayout = QtGui.QHBoxLayout()
        self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
        spacerItem = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem)
        self.btnLogin = QtGui.QPushButton(frmLogin)
        self.btnLogin.setStyleSheet(_fromUtf8("QPushButton{\n"
"    background-color: rgb(221, 221, 221)\n"
"}"))
        self.btnLogin.setObjectName(_fromUtf8("btnLogin"))
        self.horizontalLayout.addWidget(self.btnLogin)
        self.btnSignUp = QtGui.QPushButton(frmLogin)
        self.btnSignUp.setStyleSheet(_fromUtf8("QPushButton {\n"
"    background-color: rgb(221, 221, 221)\n"
"}"))
        self.btnSignUp.setObjectName(_fromUtf8("btnSignUp"))
        self.horizontalLayout.addWidget(self.btnSignUp)
        self.btnCancel = QtGui.QPushButton(frmLogin)
        self.btnCancel.setStyleSheet(_fromUtf8("QPushButton {\n"
"    background-color: rgb(221, 221, 221)\n"
"}"))
        self.btnCancel.setObjectName(_fromUtf8("btnCancel"))
        self.horizontalLayout.addWidget(self.btnCancel)
        spacerItem1 = QtGui.QSpacerItem(40, 20, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)
        self.horizontalLayout.addItem(spacerItem1)
        self.verticalLayout.addLayout(self.horizontalLayout)
        spacerItem2 = QtGui.QSpacerItem(0, 0, QtGui.QSizePolicy.Minimum, QtGui.QSizePolicy.Expanding)
        self.verticalLayout.addItem(spacerItem2)

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

    def retranslateUi(self, frmLogin):
        frmLogin.setWindowTitle(_translate("frmLogin", "Login", None))
        self.label.setText(_translate("frmLogin", "LOGIN FORM", None))
        self.lblUsername.setText(_translate("frmLogin", "USERNAME", None))
        self.lblPassword.setText(_translate("frmLogin", "PASSWORD", None))
        self.btnLogin.setText(_translate("frmLogin", "Login", None))
        self.btnSignUp.setText(_translate("frmLogin", "Sign Up", None))
        self.btnCancel.setText(_translate("frmLogin", "Cancel", None))
        self.btnCancel.clicked.connect(QtCore.QCoreApplication.instance().quit)



if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)      
    frmLogin = QtGui.QDialog()              
    ui = Ui_frmLogin()
    ui.setupUi(frmLogin)
    frmLogin.show()                        
    sys.exit(app.exec_())

Tags: selflabeltranslatefontqtguiqtcoresetobjectnameaddwidget
1条回答
网友
1楼 · 发布于 2024-10-03 11:14:32

如果“取消”按钮的目的是关闭窗体,则可以更改:

self.btnCancel.clicked.connect(QtCore.QCoreApplication.instance().quit)

使用:

    self.btnCancel.clicked.connect(self.ExitForm)

def ExitForm(self):
        sys.exit()

相关问题 更多 >