这是使用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.okButton.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?还是有更好的代码来区分“确定”和“取消”命令?
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