如何在 PyQt5 中的消息框后保持对话框打开?
How to keep the dialog open after message box in PyQt5?
我有一个名为“第一页”的 QmainWindow,其中有两个按钮“登录”和“注册”。这两个按钮都会打开 Qdialogs。在我的登录对话框中,我有两行接受用户输入的编辑和两个按钮“取消”和“确定”。单击“确定”后,我想检查一些验证,例如如果行编辑为空,则在对话框顶部显示一个 QmessageBox,然后在用户单击消息框上的“确定”后关闭消息框。
我的问题:消息框打开时,对话框关闭。
P.S:我一直在通过修改 QtDesigner 代码来实现我的代码,我刚刚发现这是最糟糕的事情!所以我又做了一切。我当时没有这个问题。
我的代码:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from first_page import Ui_first_page
from signin_page import Ui_signin_page
from signup_page import Ui_signup_page
class SignIn(qtw.QDialog):
def __init__(self, *args, **kwargs):
"""SignIn constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_signin_page()
self.ui.setupUi(self)
# Connects the function that inputs data for sign in to the OK button.
self.ui.signin_buttons.accepted.connect(self.sign_in_authenticate)
def sign_in_authenticate(self):
"""Inputs the user's username and password for sign in process and checks validations"""
input_username = self.ui.signin_username_lineEdit.text()
input_password = self.ui.signin_password_lineEdit.text()
# Check if both are filled
if input_username == "":
#MY PROBLEM IS HERE
qtw.QMessageBox.critical(self,"Note", "Please input your username.")
elif input_password == "":
qtw.QMessageBox.critical(self, "Note", "Please input your password.")
else:
# Checks validations and opens mainpage
open_main_page(self, username, status)
def open_main_page(self, username, status):
""" Opens the main page after sign in"""
pass
class FirstPage(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
"""FirstPage constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_first_page()
self.ui.setupUi(self)
# Connecting the buttons to open Sign in and Sign out pages
self.ui.first_page_sign_in_button.clicked.connect(self.open_sign_in_page)
self.ui.first_page_sign_up_button.clicked.connect(self.open_sign_up_page)
self.show()
def open_sign_in_page(self):
self.sigin = SignIn()
self.sigin.show()
def open_sign_up_page(self):
self.signup = SignUp()
self.signup.show()
class SignUp(qtw.QDialog):
#Doesn't do anything yet.
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ui = Ui_signup_page()
self.ui.setupUi(self)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = FirstPage()
sys.exit(app.exec_())
我是新手所以如果我做错了什么请指出。
提前致谢。
我的问题的小例子:
主要代码:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from mainwindow import Ui_MainWindow
from dialog import Ui_signin_Dialog
class SignIn(qtw.QDialog):
def __init__(self, *args, **kwargs):
"""SignIn constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_signin_Dialog()
self.ui.setupUi(self)
# Connects the function that inputs data for sign in to the OK button.
self.ui.signin_buttonBox.accepted.connect(self.sign_in_authenticate)
def sign_in_authenticate(self):
input_text = self.ui.input_lineEdit.text()
if input_text == "":
qtw.QMessageBox.critical(self,"Note", "Please input your data.")
class FirstPage(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
"""FirstPage constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connecting the buttons to open Sign in and Sign out pages
self.ui.signin_pushButton.clicked.connect(self.open_sign_in_page)
self.show()
def open_sign_in_page(self):
self.sigin = SignIn()
self.sigin.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = FirstPage()
sys.exit(app.exec_())
Ui_MainWindow:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(311, 293)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.signin_pushButton = QtWidgets.QPushButton(self.centralwidget)
self.signin_pushButton.setGeometry(QtCore.QRect(100, 110, 89, 25))
self.signin_pushButton.setObjectName("signin_pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 311, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.signin_pushButton.setText(_translate("MainWindow", "Sign in "))
Ui_signin_Dialog
class Ui_signin_Dialog(object):
def setupUi(self, signin_Dialog):
signin_Dialog.setObjectName("signin_Dialog")
signin_Dialog.resize(400, 300)
self.signin_buttonBox = QtWidgets.QDialogButtonBox(signin_Dialog)
self.signin_buttonBox.setGeometry(QtCore.QRect(290, 230, 80, 56))
self.signin_buttonBox.setOrientation(QtCore.Qt.Vertical)
self.signin_buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.signin_buttonBox.setObjectName("signin_buttonBox")
self.input_lineEdit = QtWidgets.QLineEdit(signin_Dialog)
self.input_lineEdit.setGeometry(QtCore.QRect(140, 130, 113, 25))
self.input_lineEdit.setObjectName("input_lineEdit")
self.retranslateUi(signin_Dialog)
self.signin_buttonBox.accepted.connect(signin_Dialog.accept)
self.signin_buttonBox.rejected.connect(signin_Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(signin_Dialog)
def retranslateUi(self, signin_Dialog):
_translate = QtCore.QCoreApplication.translate
signin_Dialog.setWindowTitle(_translate("signin_Dialog", "Dialog"))
我希望 Qmessagebox 像弹出消息一样打开而不是关闭对话框。
试一试:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore, QtGui, QtWidgets # +++
#from mainwindow import Ui_MainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(311, 293)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.signin_pushButton = QtWidgets.QPushButton(self.centralwidget)
self.signin_pushButton.setGeometry(QtCore.QRect(100, 110, 89, 25))
self.signin_pushButton.setObjectName("signin_pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 311, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.signin_pushButton.setText(_translate("MainWindow", "Sign in "))
#from dialog import Ui_signin_Dialog
class Ui_signin_Dialog(object):
def setupUi(self, signin_Dialog):
signin_Dialog.setObjectName("signin_Dialog")
signin_Dialog.resize(400, 300)
self.signin_buttonBox = QtWidgets.QDialogButtonBox(signin_Dialog)
self.signin_buttonBox.setGeometry(QtCore.QRect(290, 230, 80, 56))
self.signin_buttonBox.setOrientation(QtCore.Qt.Vertical)
self.signin_buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.signin_buttonBox.setObjectName("signin_buttonBox")
self.input_lineEdit = QtWidgets.QLineEdit(signin_Dialog)
self.input_lineEdit.setGeometry(QtCore.QRect(140, 130, 113, 25))
self.input_lineEdit.setObjectName("input_lineEdit")
self.retranslateUi(signin_Dialog)
# self.signin_buttonBox.accepted.connect(signin_Dialog.accept) # ---
self.signin_buttonBox.rejected.connect(signin_Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(signin_Dialog)
def retranslateUi(self, signin_Dialog):
_translate = QtCore.QCoreApplication.translate
signin_Dialog.setWindowTitle(_translate("signin_Dialog", "Dialog"))
class SignIn(qtw.QDialog):
def __init__(self, *args, **kwargs):
"""SignIn constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_signin_Dialog()
self.ui.setupUi(self)
# Connects the function that inputs data for sign in to the OK button.
self.ui.signin_buttonBox.accepted.connect(self.sign_in_authenticate)
def sign_in_authenticate(self):
input_text = self.ui.input_lineEdit.text()
if input_text == "":
qtw.QMessageBox.critical(self,"Note", "Please input your data.")
else: # +++
self.accept() # +++
class FirstPage(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
"""FirstPage constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connecting the buttons to open Sign in and Sign out pages
self.ui.signin_pushButton.clicked.connect(self.open_sign_in_page)
self.show()
def open_sign_in_page(self):
self.sigin = SignIn()
self.sigin.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = FirstPage()
sys.exit(app.exec_())
我有一个名为“第一页”的 QmainWindow,其中有两个按钮“登录”和“注册”。这两个按钮都会打开 Qdialogs。在我的登录对话框中,我有两行接受用户输入的编辑和两个按钮“取消”和“确定”。单击“确定”后,我想检查一些验证,例如如果行编辑为空,则在对话框顶部显示一个 QmessageBox,然后在用户单击消息框上的“确定”后关闭消息框。
我的问题:消息框打开时,对话框关闭。 P.S:我一直在通过修改 QtDesigner 代码来实现我的代码,我刚刚发现这是最糟糕的事情!所以我又做了一切。我当时没有这个问题。
我的代码:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from first_page import Ui_first_page
from signin_page import Ui_signin_page
from signup_page import Ui_signup_page
class SignIn(qtw.QDialog):
def __init__(self, *args, **kwargs):
"""SignIn constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_signin_page()
self.ui.setupUi(self)
# Connects the function that inputs data for sign in to the OK button.
self.ui.signin_buttons.accepted.connect(self.sign_in_authenticate)
def sign_in_authenticate(self):
"""Inputs the user's username and password for sign in process and checks validations"""
input_username = self.ui.signin_username_lineEdit.text()
input_password = self.ui.signin_password_lineEdit.text()
# Check if both are filled
if input_username == "":
#MY PROBLEM IS HERE
qtw.QMessageBox.critical(self,"Note", "Please input your username.")
elif input_password == "":
qtw.QMessageBox.critical(self, "Note", "Please input your password.")
else:
# Checks validations and opens mainpage
open_main_page(self, username, status)
def open_main_page(self, username, status):
""" Opens the main page after sign in"""
pass
class FirstPage(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
"""FirstPage constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_first_page()
self.ui.setupUi(self)
# Connecting the buttons to open Sign in and Sign out pages
self.ui.first_page_sign_in_button.clicked.connect(self.open_sign_in_page)
self.ui.first_page_sign_up_button.clicked.connect(self.open_sign_up_page)
self.show()
def open_sign_in_page(self):
self.sigin = SignIn()
self.sigin.show()
def open_sign_up_page(self):
self.signup = SignUp()
self.signup.show()
class SignUp(qtw.QDialog):
#Doesn't do anything yet.
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.ui = Ui_signup_page()
self.ui.setupUi(self)
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = FirstPage()
sys.exit(app.exec_())
我是新手所以如果我做错了什么请指出。 提前致谢。
我的问题的小例子:
主要代码:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from mainwindow import Ui_MainWindow
from dialog import Ui_signin_Dialog
class SignIn(qtw.QDialog):
def __init__(self, *args, **kwargs):
"""SignIn constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_signin_Dialog()
self.ui.setupUi(self)
# Connects the function that inputs data for sign in to the OK button.
self.ui.signin_buttonBox.accepted.connect(self.sign_in_authenticate)
def sign_in_authenticate(self):
input_text = self.ui.input_lineEdit.text()
if input_text == "":
qtw.QMessageBox.critical(self,"Note", "Please input your data.")
class FirstPage(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
"""FirstPage constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connecting the buttons to open Sign in and Sign out pages
self.ui.signin_pushButton.clicked.connect(self.open_sign_in_page)
self.show()
def open_sign_in_page(self):
self.sigin = SignIn()
self.sigin.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = FirstPage()
sys.exit(app.exec_())
Ui_MainWindow:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(311, 293)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.signin_pushButton = QtWidgets.QPushButton(self.centralwidget)
self.signin_pushButton.setGeometry(QtCore.QRect(100, 110, 89, 25))
self.signin_pushButton.setObjectName("signin_pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 311, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.signin_pushButton.setText(_translate("MainWindow", "Sign in "))
Ui_signin_Dialog
class Ui_signin_Dialog(object):
def setupUi(self, signin_Dialog):
signin_Dialog.setObjectName("signin_Dialog")
signin_Dialog.resize(400, 300)
self.signin_buttonBox = QtWidgets.QDialogButtonBox(signin_Dialog)
self.signin_buttonBox.setGeometry(QtCore.QRect(290, 230, 80, 56))
self.signin_buttonBox.setOrientation(QtCore.Qt.Vertical)
self.signin_buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.signin_buttonBox.setObjectName("signin_buttonBox")
self.input_lineEdit = QtWidgets.QLineEdit(signin_Dialog)
self.input_lineEdit.setGeometry(QtCore.QRect(140, 130, 113, 25))
self.input_lineEdit.setObjectName("input_lineEdit")
self.retranslateUi(signin_Dialog)
self.signin_buttonBox.accepted.connect(signin_Dialog.accept)
self.signin_buttonBox.rejected.connect(signin_Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(signin_Dialog)
def retranslateUi(self, signin_Dialog):
_translate = QtCore.QCoreApplication.translate
signin_Dialog.setWindowTitle(_translate("signin_Dialog", "Dialog"))
我希望 Qmessagebox 像弹出消息一样打开而不是关闭对话框。
试一试:
import sys
from PyQt5 import QtWidgets as qtw
from PyQt5 import QtCore as qtc
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore, QtGui, QtWidgets # +++
#from mainwindow import Ui_MainWindow
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(311, 293)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.signin_pushButton = QtWidgets.QPushButton(self.centralwidget)
self.signin_pushButton.setGeometry(QtCore.QRect(100, 110, 89, 25))
self.signin_pushButton.setObjectName("signin_pushButton")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 311, 22))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.signin_pushButton.setText(_translate("MainWindow", "Sign in "))
#from dialog import Ui_signin_Dialog
class Ui_signin_Dialog(object):
def setupUi(self, signin_Dialog):
signin_Dialog.setObjectName("signin_Dialog")
signin_Dialog.resize(400, 300)
self.signin_buttonBox = QtWidgets.QDialogButtonBox(signin_Dialog)
self.signin_buttonBox.setGeometry(QtCore.QRect(290, 230, 80, 56))
self.signin_buttonBox.setOrientation(QtCore.Qt.Vertical)
self.signin_buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel|QtWidgets.QDialogButtonBox.Ok)
self.signin_buttonBox.setObjectName("signin_buttonBox")
self.input_lineEdit = QtWidgets.QLineEdit(signin_Dialog)
self.input_lineEdit.setGeometry(QtCore.QRect(140, 130, 113, 25))
self.input_lineEdit.setObjectName("input_lineEdit")
self.retranslateUi(signin_Dialog)
# self.signin_buttonBox.accepted.connect(signin_Dialog.accept) # ---
self.signin_buttonBox.rejected.connect(signin_Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(signin_Dialog)
def retranslateUi(self, signin_Dialog):
_translate = QtCore.QCoreApplication.translate
signin_Dialog.setWindowTitle(_translate("signin_Dialog", "Dialog"))
class SignIn(qtw.QDialog):
def __init__(self, *args, **kwargs):
"""SignIn constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_signin_Dialog()
self.ui.setupUi(self)
# Connects the function that inputs data for sign in to the OK button.
self.ui.signin_buttonBox.accepted.connect(self.sign_in_authenticate)
def sign_in_authenticate(self):
input_text = self.ui.input_lineEdit.text()
if input_text == "":
qtw.QMessageBox.critical(self,"Note", "Please input your data.")
else: # +++
self.accept() # +++
class FirstPage(qtw.QMainWindow):
def __init__(self, *args, **kwargs):
"""FirstPage constructor."""
super().__init__(*args, **kwargs)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
# Connecting the buttons to open Sign in and Sign out pages
self.ui.signin_pushButton.clicked.connect(self.open_sign_in_page)
self.show()
def open_sign_in_page(self):
self.sigin = SignIn()
self.sigin.show()
if __name__ == '__main__':
app = qtw.QApplication(sys.argv)
window = FirstPage()
sys.exit(app.exec_())