我在使用PyQT创建GUI时遇到问题,我已附上代码供您参考。问题是每当显示一个消息框时,我都无法点击OK或者' x'关闭它。谁能告诉我如何处理它?非常感谢你。 有没有人有答案,我非常需要它。
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'button.ui'
#
# Created by: PyQt5 UI code generator 5.9.2
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
import os
class Ui_SubWindow(object):
def showMessageBox(self, title, message):
msgBox=QMessageBox()
msgBox.setIcon(QMessageBox.Warning)
msgBox.setWindowTitle(title)
msgBox.setText(message)
msgBox.setStandardButtons(QMessageBox.Ok)
msgBox.exec_()
def checking_name(self):
firstname=self.lineEdit.text()
lastname=self.lineEdit_2.text()
b=os.listdir(r'C:\Users\dngochuynh\PycharmProjects\fr-project\standalone\image folder\downsized image')
for e in b:
if str(firstname).lower() in e:
if str(lastname).lower() in e:
os.remove(r'C:\Users\dngochuynh\PycharmProjects\fr-project\standalone\image folder\downsized image\{}'.format(e))
self.showMessageBox('Warning','Image is deleted')
else:
self.showMessageBox('Warning','Cannot find the image, please check if your typing is correct')
def setupUi(self, SubWindow):
SubWindow.setObjectName("SubWindow")
SubWindow.resize(350, 307)
SubWindow.setStyleSheet("")
self.centralwidget = QtWidgets.QWidget(SubWindow)
self.centralwidget.setObjectName("centralwidget")
self.forgetButton = QtWidgets.QPushButton(self.centralwidget)
self.forgetButton.setGeometry(QtCore.QRect(120, 230, 111, 51))
font = QtGui.QFont()
font.setPointSize(10)
self.forgetButton.setFont(font)
self.forgetButton.setObjectName("forgetButton")
####################################################
self.forgetButton.clicked.connect(self.checking_name)
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(10, 20, 71, 31))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.centralwidget)
self.label_2.setGeometry(QtCore.QRect(10, 60, 71, 31))
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.centralwidget)
self.label_3.setGeometry(QtCore.QRect(10, 140, 321, 61))
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setGeometry(QtCore.QRect(10, 110, 331, 111))
self.textBrowser.setObjectName("textBrowser")
self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit.setGeometry(QtCore.QRect(100, 20, 221, 31))
self.lineEdit.setObjectName("lineEdit")
self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_2.setGeometry(QtCore.QRect(100, 60, 221, 31))
self.lineEdit_2.setObjectName("lineEdit_2")
SubWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(SubWindow)
self.statusbar.setObjectName("statusbar")
SubWindow.setStatusBar(self.statusbar)
self.retranslateUi(SubWindow)
QtCore.QMetaObject.connectSlotsByName(SubWindow)
def retranslateUi(self, SubWindow):
_translate = QtCore.QCoreApplication.translate
SubWindow.setWindowTitle(_translate("SubWindow", "MainWindow"))
self.forgetButton.setText(_translate("SubWindow", "Delete My Image"))
self.label.setText(_translate("SubWindow", "First Name"))
self.label_2.setText(_translate("SubWindow", "Last Name"))
self.textBrowser.setHtml(_translate("SubWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
"<html><head><meta name=\"qrichtext\" content=\"1\" /><style type=\"text/css\">\n"
"p, li { white-space: pre-wrap; }\n"
"</style></head><body style=\" font-family:\'MS Shell Dlg 2\'; font-size:8.25pt; font-weight:400; font-style:normal;\">\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">If because of privacy reason, you want to delete your image from the dabase. Please forllow the instruction:</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">1. Enter your first name</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">2. Enter your last name</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">3. Press Delete My Image</p>\n"
"<p style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\">After that your image will be deleted from the database and the program will not recognize you, next time it running. </p></body></html>"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
SubWindow = QtWidgets.QMainWindow()
ui = Ui_SubWindow()
ui.setupUi(SubWindow)
SubWindow.show()
sys.exit(app.exec_())
答案 0 :(得分:1)
我认为问题不在于消息框不会关闭,而是在关闭后立即重新打开。编写for语句的方式,将为目录中的每个文件显示一个消息框,无论它是否包含名字和姓氏。
答案 1 :(得分:0)
尝试替换`checking_name'
方法...
def checking_name(self):
firstname = str(self.lineEdit.text()).lower()
lastname = str(self.lineEdit_2.text()).lower()
b=os.listdir(r'C:\Users\dngochuynh\PycharmProjects\fr-project\standalone\image folder\downsized image')
#for e in b:
if (firstname in b) and firstname:
if (lastname in b) and lastname:
os.remove(r'C:\Users\dngochuynh\PycharmProjects\fr-project\standalone\image folder\downsized image\{}'.format(lastname))
self.showMessageBox('Warning','{} Image is deleted'.format(lastname))
self.lineEdit.setText("")
self.lineEdit_2.setText("")
else:
self.showMessageBox('Warning','firstname != lastname')
else:
self.showMessageBox('Warning',
'Cannot find the `{}` image, please check if your typing is correct'\
.format(firstname))
...
答案 2 :(得分:0)
感谢您的所有答案,这是我修复代码的方式
def checking_name(self):
firstname=self.lineEdit.text()
lastname=self.lineEdit_2.text()
os.chdir(r'C:\Users\dngochuynh\PycharmProjects\fr-project\standalone\image folder\downsize image2')
list_image=os.listdir(r'C:\Users\dngochuynh\PycharmProjects\fr-project\standalone\image folder\downsize image2')
for e in list_image:
if (str(firstname).lower() in str(e).lower()) and (str(lastname).lower() in str(e).lower()):
os.remove(r'C:\Users\dngochuynh\PycharmProjects\fr-project\standalone\image folder\downsize image2\{}'.format(e))
self.showMessageBox('Confirmation','Image is deleted')
break
else:
self.showMessageBox('Warning','Cannot find the image, please check if your typing is correct')