这是使用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”时,如何获得“返回acceptCommand”?
还是有更好的代码来区分ok和cancel命令?
最佳答案
解决方案是创建类的属性,该属性可在按下信息时保存该信息,并在以后使用:
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
关于python - PyQt5区分确定和取消命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59125441/