Python 弹出窗口失败
我正在尝试制作一个登录界面,当我点击“登录”时,如果信息正确,将进入另一个窗口。主窗口和对话框可以单独显示,但分组后,总是弹出对话框失败。这个问题困扰了我很长时间,如果有人能为我解答,我将非常感激和高兴,非常感谢。这是我的代码:Python 弹出窗口失败,python,pyqt5,Python,Pyqt5,我正在尝试制作一个登录界面,当我点击“登录”时,如果信息正确,将进入另一个窗口。主窗口和对话框可以单独显示,但分组后,总是弹出对话框失败。这个问题困扰了我很长时间,如果有人能为我解答,我将非常感激和高兴,非常感谢。这是我的代码: from PyQt5 import QtCore, QtGui, QtWidgets import sys from PyQt5.QtWidgets import * class Ui_MainWindow(object): def setupUi(self
from PyQt5 import QtCore, QtGui, QtWidgets
import sys
from PyQt5.QtWidgets import *
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(640, 480)
font = QtGui.QFont()
font.setFamily("Bell MT")
font.setPointSize(12)
MainWindow.setFont(font)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(60, 120, 131, 31))
font = QtGui.QFont()
font.setFamily("Jokerman")
font.setPointSize(16)
self.label.setFont(font)
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(70, 190, 111, 20))
font = QtGui.QFont()
font.setFamily("Jokerman")
font.setPointSize(16)
self.label_2.setFont(font)
self.label_2.setObjectName("label_2")
self.user_name = QtWidgets.QLineEdit(self.centralwidget)
self.user_name.setGeometry(QtCore.QRect(200, 120, 301, 41))
self.user_name.setObjectName("user_name")
self.password = QtWidgets.QLineEdit(self.centralwidget)
self.password.setGeometry(QtCore.QRect(200, 180, 301, 41))
self.password.setEchoMode(QtWidgets.QLineEdit.Password)
self.password.setObjectName("password")
self.login = QtWidgets.QPushButton(self.centralwidget)
self.login.setGeometry(QtCore.QRect(300, 260, 101, 51))
font = QtGui.QFont()
font.setFamily("Jokerman")
font.setPointSize(16)
self.login.setFont(font)
self.login.setObjectName("login")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(240, 30, 191, 31))
font = QtGui.QFont()
font.setFamily("Jokerman")
font.setPointSize(18)
self.label_3.setFont(font)
self.label_3.setObjectName("label_3")
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.login.clicked.connect(self.accept)
self.login.clicked.connect(MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "user name:"))
self.label_2.setText(_translate("MainWindow", "password:"))
self.login.setText(_translate("MainWindow", "login"))
self.label_3.setText(_translate("MainWindow", "Market storage"))
def accept(self):
if self.user_name.text() == "123" and self.password.text() == "123":
app = QApplication(sys.argv)
dialog = QDialog()
ui = Ui_Dialog()
ui.setupUi(dialog)
dialog.show()
sys.exit(app.exec_())
else:
print("wrong imformation")
class Ui_Dialog(QDialog):
def __init__(self, parent=None):
super(Ui_Dialog, self).__init__(parent)
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(640, 480)
self.buttonBox = QtWidgets.QDialogButtonBox(Dialog)
self.buttonBox.setGeometry(QtCore.QRect(-50, 250, 621, 31))
self.buttonBox.setOrientation(QtCore.Qt.Horizontal)
self.buttonBox.setStandardButtons(QtWidgets.QDialogButtonBox.Cancel | QtWidgets.QDialogButtonBox.Ok)
self.buttonBox.setObjectName("buttonBox")
self.label = QtWidgets.QLabel(Dialog)
self.label.setGeometry(QtCore.QRect(210, 140, 291, 81))
font = QtGui.QFont()
font.setFamily("Agency FB")
font.setPointSize(20)
self.label.setFont(font)
self.label.setObjectName("label")
self.retranslateUi(Dialog)
self.buttonBox.accepted.connect(Dialog.accept)
self.buttonBox.rejected.connect(Dialog.reject)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label.setText(_translate("Dialog", "the second windows"))
if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
这个问题主要是因为您只需要有一个QApplication,所以当您创建另一个QApplication时,您会得到意外的行为。此外,该任务很快结束,因此当对话框是局部变量时,将删除该对话框,因此窗口也将关闭,以便不会发生此情况,您必须使对话框成为该类的成员
def accept(self):
if self.user_name.text() == "123" and self.password.text() == "123":
self.dialog = QDialog()
ui = Ui_Dialog()
ui.setupUi(self.dialog)
self.dialog.show()
else:
print("wrong imformation")
弹出对话框失败会发生什么?你有错误吗?哪一个?没有错误,只是用退出代码完成了disply进程-1问题解决了!衷心感谢您的回答。昨晚我做梦的时候梦见有人回答了我的问题。我没想到这是真的。这是我关于堆栈溢出的第一个问题。我深深感受到了这种力量和温暖。我非常感谢您的帮助和帮助我想我已经爱上了堆栈溢出。哈哈哈哈~@Amy如果我的答案对你有帮助,别忘了把它标记为正确的,如果你不知道怎么做,请检查:哈哈哈~~你太好了,你又教我了,谢谢D@Amy如果您不想受到限制(例如修订队列中的帮助),我建议您赢得声誉。有关更多信息,请查看: