1

这是使用 PyQt5 QDialog 的对话框代码。

class QDialogUI(QDialog):
    def __init__(self):

        super().__init__()

        self.okButton = QPushButton("Ok", self)
        self.okButton.clicked.connect(self.acceptCommand)
        self.okButton.clicked.connect(lambda:self.closeCommand(1))

        self.cancelButton = QPushButton("Cancel", self)
        self.cancelButton.clicked.connect(lambda:self.closeCommand(0))

    def acceptCommand(self):
        ...
        return date, asset, sort, money, text

    def closeCommand(self, status):
        return status

这是主要代码。

def openDialog(self):
    self.dlg = QDialogUI()
    self.dlg.exec_()
    if self.dlg.closeCommand() == 1:
        iD = list(self.dlg.acceptCommand())
        self.params.emit(iD[0],iD[1],iD[2],iD[3],iD[4])

如果我单击 okButton 或 cancelButton,它们都没有反应。然后我关闭 QDialogUI,它显示如下错误:

TypeError: closeCommand()missing 1 required positional argument: 'status'

当'okButton.clicked'时,我怎样才能得到'return of acceptCommand'?
还是有更好的代码来区分 ok 和 cancel 命令?

4

1 回答 1

1

解决方案是创建一个类的属性,该属性在按下时保存该信息并且可以在以后使用:

class QDialogUI(QDialog):
    def __init__(self):
        super().__init__()
        self.status = None
        self.okButton = QPushButton("Ok", self)
        self.okButton.clicked.connect(self.acceptCommand)
        self.okButton.clicked.connect(lambda:self.closeCommand(1))

        self.cancelButton = QPushButton("Cancel", self)
        self.okButton.clicked.connect(lambda:self.closeCommand(0))

    def acceptCommand(self):
        ...
        self.status = date, asset, sort, money, text

    def closeCommand(self, status):
        return self.status
于 2019-12-01T12:45:10.773 回答