from PyQt5.QtWidgets import ( QDialog, QDialogButtonBox, QVBoxLayout, QLabel, QGroupBox, QGridLayout, QLineEdit, ) from PyQt5 import QtCore class Parameter: def __init__(self, name: str, default_value: str, widget: QLineEdit): self.name = name self.defaultValue = default_value self.widget = widget self.value = self.defaultValue self.widget.setPlaceholderText(self.defaultValue) def reset(self): self.value = self.defaultValue self.widget.setPlaceholderText(self.defaultValue) self.widget.setText("") class ParameterDialog(QDialog): def __init__(self): super().__init__() self.setWindowTitle("Enter Parameters") buttons = QDialogButtonBox.Ok | QDialogButtonBox.Cancel | QDialogButtonBox.Reset self.buttonBox = QDialogButtonBox(buttons) self.buttonBox.accepted.connect(self.accept) self.buttonBox.rejected.connect(self.reject) self.buttonBox.button(QDialogButtonBox.Reset).clicked.connect(self._reset) self.data = 1 self.rootLayout = QVBoxLayout() self.group = QGroupBox("Parameters") self.rootLayout.addWidget(self.group) self.rootLayout.addWidget(self.buttonBox) self.setLayout(self.rootLayout) self.groupLayout = QGridLayout() self.group.setLayout(self.groupLayout) self.parameters = {} def add_parameter(self, name: str, default_value: str): row = self.groupLayout.rowCount() + 1 description = QLabel(name) self.groupLayout.addWidget(description, row, 0) value_widget = QLineEdit() self.groupLayout.addWidget(value_widget, row, 1) parameter = Parameter(name, default_value, value_widget) self.parameters[name] = parameter def _reset(self): for value in self.parameters.values(): value.reset() def get_parameters(self): output = {} for key, parameter in self.parameters.items(): if parameter.widget is not None: if parameter.widget.text() != "": parameter.value = parameter.widget.text() output[key] = parameter.value return output @QtCore.pyqtSlot() def reject(self): print("reject") self._reset() super().reject() """Prompt the user to specify additional Parameters :param parameterList: array of dictionaries with name and defaultValue attributes :return: dict with all names as key and user supplied input as value string """ def prompt_parameters_gui(param_list) -> dict: return _gui_prompt(param_list) def prompt_parameters_cli(param_list) -> dict: return _cli_prompt(param_list) def _gui_prompt(param_list) -> dict: dialog = ParameterDialog() for parameter in param_list: dialog.add_parameter(parameter["name"], parameter["defaultValue"]) dialog.exec_() return dialog.get_parameters() def _cli_prompt(param_list) -> dict: result = {} for parameter in param_list: user_input = input( "Specify {} [{}]: ".format(parameter["name"], parameter["defaultValue"]) ) if user_input == "": user_input = parameter["defaultValue"] result[parameter["name"]] = user_input return result