Python PySide2:使用按钮启动QDialog并等待响应

时间:2019-07-08 16:19:54

标签: python pyside2

我正在尝试让一个应用程序以用户名/密码对话框开头。用户输入用户名/密码后,将出现主对话框。我的问题是该对话框没有被阻止,因此出现了登录提示,但主窗口也出现了。如何让应用程序等待对话框中的输入再继续?

此外,如何获取在提示中输入的用户名和密码?我是否必须创建一个插槽来将值存储在某个地方?

我可能会完全错误地创建/显示登录窗口,所以请随意引导我转向另一个方向。

main.py

from PySide2.QtWidgets import QDialog, QApplication, QMainWindow
from loginPrompt import *

app = QApplication(sys.argv)   

login = QDialog()
login.ui = Ui_Login()
login.ui.setupUi(login)

login.setWindowModality(QtCore.Qt.ApplicationModal)

login.show()           # This shows the login prompt

w = MyForm(arguments)  # This creates and shows the main part of my application
w.show()

sys.exit(app.exec_())

loginPrompt.py

from PySide2 import QtCore, QtGui, QtWidgets

class Ui_Login(object):
    def setupUi(self, Login):
        Login.setObjectName("Login")
        Login.resize(471, 113)
        self.formLayout = QtWidgets.QFormLayout(Login)
        self.formLayout.setObjectName("formLayout")
        self.label = QtWidgets.QLabel(Login)
        self.label.setObjectName("label")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.LabelRole, self.label)
        self.le_username = QtWidgets.QLineEdit(Login)
        self.le_username.setObjectName("le_username")
        self.formLayout.setWidget(0, QtWidgets.QFormLayout.FieldRole, self.le_username)
        self.label_2 = QtWidgets.QLabel(Login)
        self.label_2.setObjectName("label_2")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.LabelRole, self.label_2)
        self.le_password = QtWidgets.QLineEdit(Login)
        self.le_password.setEchoMode(QtWidgets.QLineEdit.Password)
        self.le_password.setObjectName("le_password")
        self.formLayout.setWidget(1, QtWidgets.QFormLayout.FieldRole, self.le_password)
        self.buttonBox = QtWidgets.QDialogButtonBox(Login)
        self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
        self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
        self.buttonBox.setObjectName("buttonBox")
        self.formLayout.setWidget(2, QtWidgets.QFormLayout.SpanningRole, self.buttonBox)

        self.retranslateUi(Login)
        self.buttonBox.accepted.connect(Login.accept)
        self.buttonBox.rejected.connect(Login.reject)
        QtCore.QMetaObject.connectSlotsByName(Login)

    def retranslateUi(self, Login):
        _translate = QtCore.QCoreApplication.translate
        Login.setWindowTitle(_translate("Login", "Dialog"))
        self.label.setText(_translate("Login", "Username"))
        self.label_2.setText(_translate("Login", "Password"))

代码在login.show()之后继续执行。我需要它在那里等待直到按下其中一个按钮(确定或取消)。

0 个答案:

没有答案