Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQt5区分确定和取消命令_Python_Python 3.x_Pyqt_Pyqt5 - Fatal编程技术网

Python PyQt5区分确定和取消命令

Python PyQt5区分确定和取消命令,python,python-3.x,pyqt,pyqt5,Python,Python 3.x,Pyqt,Pyqt5,这是使用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(lam

这是使用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])
如果我单击“确定”按钮或“取消”按钮,它们都不会做出反应。我关闭QDialogUI,它会显示如下错误:

TypeError: closeCommand()missing 1 required positional argument: 'status'
当“确定按钮.单击”时,如何获取“返回acceptCommand”?

还是有更好的代码来区分“确定”和“取消”命令?

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

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
QDialogUI类(QDialog):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.status=None
self.okButton=QPushButton(“确定”,self)
self.okButton.clicked.connect(self.acceptCommand)
self.okButton.clicked.connect(lambda:self.closeCommand(1))
self.cancelButton=QPushButton(“取消”,self)
self.okButton.clicked.connect(lambda:self.closeCommand(0))
def接受命令(自我):
...
self.status=日期、资产、排序、货币、文本
def CLOSE命令(自身,状态):

返回self.status
Typo:change
self.okButton.clicked.connect(lambda:closeCommand(0))
to
self.okButton.clicked.connect(lambda:self.closeCommand(0))
这是我的错误..抱歉…另一个输入错误:在
self.okButton=QPushButton(“Ok”,self)
之前添加
super()!成功了。我想不起来了。谢谢你的回答