QMessageBox上按钮的buttonClicked()方法

2024-10-05 14:27:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在单击QMessageBox上的“Ok”按钮时调用“RemovedUpplicate”方法。但是,当我单击按钮时,该方法不会执行。我该怎么办? 以下是我的代码片段:

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)

Tags: 方法代码selfdefokmsg按钮qtwidgets
1条回答
网友
1楼 · 发布于 2024-10-05 14:27:06

试试看:

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_())        

相关问题 更多 >