python - QMessageBox 上按钮的 buttonClicked() 方法
问题描述
当单击 QMessageBox 上的“确定”按钮时,我想调用“removeDuplicate”方法。但是当我单击按钮时,该方法不会执行。我应该怎么办?这是我的代码片段:
def removeDuplicate(self):
curItem = self.listWidget_2.currentItem()
self.listWidget_2.takeItem(curItem)
def error_popup(self):
msg=QtWidgets.QMessageBox()
msg.setText("You can't select more than one wicket-keeper.")
msg.setWindowTitle(" ")
msg.setIcon(QtWidgets.QMessageBox.Critical)
x = msg.exec_()
msg.setStandardButtons(QtWidgets.QMessageBox.Ok)
msg.buttonClicked.connect(self.removeDuplicate)
解决方案
试试看:
import sys
from PyQt5.Qt import *
from PyQt5 import QtGui, QtCore, QtWidgets
class Window(QWidget):
def __init__(self):
super().__init__()
self.error_popup()
def removeDuplicate(self):
print('def removeDuplicate(self): ...')
# curItem = self.listWidget_2.currentItem()
# self.listWidget_2.takeItem(curItem)
def error_popup(self):
msg = QMessageBox.critical(
self,
'Title',
"You can't select more than one wicket-keeper",
QMessageBox.Yes | QMessageBox.Cancel
)
if msg == QMessageBox.Yes:
# msg.buttonClicked.connect(self.removeDuplicate)
print('Ok')
self.removeDuplicate()
if __name__ == "__main__":
App = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(App.exec_())
推荐阅读
- rust - 使用 actix-web 从 HTML 页面捕获 GET 和 POST 请求
- c# - 为什么“is”运算符不适用于泛型类型参数?
- css - 只要 body 和 sub sub div 滚动就缩放 div
- flutter - 基于文本长度的文本宽度
- r - 从包含使用 For 循环指定文件来源的列的多个文件创建数据框
- html - 需要帮助获得部分和子部分的正确缩进
- php - 为什么这个“if”条件不会打破循环?
- xml - 在单击按钮上更改 res/values/colors.xml
- python - 在opencv中读取、预处理和保存图像到指定位置
- c# - .NET Core + Nginx 丢失 HTTP 标头