弹出窗口失败
Posted
技术标签:
【中文标题】弹出窗口失败【英文标题】:Popup window failed 【发布时间】:2018-03-16 15:08:39 【问题描述】:我正在做一个登录界面,当我点击“登录”时,如果信息正确,会进入另一个窗口。主窗口和对话框可以单独显示,但组合在一起后,总是弹出对话框失败。这个问题很困扰我很久了,如果有人能回答我,我将非常感激和高兴,非常感谢。这是我的代码:
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_())
【问题讨论】:
"弹出对话框失败" 会发生什么?你有错误吗?哪个? 没有错误,直接显示Process finished with exit code -1
【参考方案1】:
这个问题主要是因为你只需要一个QApplication
,所以当你创建另一个时,你会得到意想不到的行为。此外,该任务很快结束,因此当 dialog
是一个局部变量时,将被删除,因此窗口也会关闭,因此您必须创建一个 dialog
类成员。
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")
【讨论】:
问题解决了!真诚的回答。昨晚做梦的时候梦见有人回答了我的问题。没想到是真的。这是我入栈的第一个问题overflow.我深深感受到了力量和温暖。非常感谢你的帮助,我想我已经爱上了***。哈哈哈哈~ @Amy 如果我的回答对您有帮助,请不要忘记将其标记为正确,如果您不知道该怎么做,请查看tour:D 哈哈哈~~你真好,你又教我了,谢谢。 :D @Amy 如果您不想受到限制,我建议您获得声誉,例如在修订队列中提供帮助,更多信息请查看:***.com/help/whats-reputation以上是关于弹出窗口失败的主要内容,如果未能解决你的问题,请参考以下文章
带有自定义单元格的 UITableView 弹出窗口失败,单元格属性始终为零
网页前端WeX5架构下,WinDialog子窗口1传递参数给主窗口关闭,再弹出子窗口2失败
更新 chrome 版本 43.0.2357.65 m 后,JQueryMobile 1.3.2 中的弹出窗口失败