【问题标题】:buttonClicked() method for button on QMessageBox [duplicate]QMessageBox上按钮的buttonClicked()方法[重复]
【发布时间】:2020-06-21 13:07:53
【问题描述】:

当点击 QMessageBox 上的“确定”按钮时,我想调用“removeDuplicate”方法。但是当我单击按钮时,该方法不会执行。我该怎么办? 这是我的代码 sn-p :

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)

【问题讨论】:

标签: python qt pyqt5 qmessagebox


【解决方案1】:

试试看:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-24
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多