diff --git a/README.md b/README.md index bdb3f10..18e4b94 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Basic Parser Usage ------------------ import argparse import sys - from PyQt4 import QtGui + from PyQt5 import QtWidgets, QtGui import argparseui # EXPERIMENT USING BASIC PARSER @@ -57,7 +57,7 @@ Basic Parser Usage group.add_argument("-q", "--quiet", action="store_true") parser.add_argument("posarg", help="positional argument", type=str) - app = QtGui.QApplication(sys.argv) + app = QtWidgets.QApplication(sys.argv) a = argparseui.ArgparseUi(parser) a.show() app.exec_() @@ -72,7 +72,7 @@ Example using save/load button and keeping the dialog open when pressing ok ----------------------------------------------------------------------------------------------------- import argparse import sys - from PyQt4 import QtGui + from PyQt5 import QtWidgets, QtGui import argparseui def do_something(argparseuiinstance): @@ -83,8 +83,8 @@ Example using save/load button and keeping the dialog open when pressing ok parser.add_argument("-m", "--make-argument-true", help="optional boolean argument", action="store_true") parser.add_argument("-o","--make-other-argument-true", help="optional boolean argument 2", action="store_true", default=True) - app = QtGui.QApplication(sys.argv) - a = argparseui.ArgparseUi(parser,use_save_load_button=True,ok_button_handler=do_something) + app = QtWidgets.QApplication(sys.argv) + a = argparseui.ArgparseUi(parser,use_save_load_button=True,ok_button_handler=do_something) a.show() app.exec_() if a.result() != 1: diff --git a/README.txt b/README.txt index 1d3f8e2..7abb1a7 100644 --- a/README.txt +++ b/README.txt @@ -26,7 +26,7 @@ from an argparse based command line tool the UI has widgets that allow to set up the command line options -argparseui depends on PyQt +argparseui depends on PyQt5 State of argparseui ------------------- @@ -43,7 +43,7 @@ Basic Parser Usage import argparse import sys - from PyQt4 import QtGui + from PyQt5 import QtWidgets, QtGui import argparseui # EXPERIMENT USING BASIC PARSER @@ -59,7 +59,7 @@ Basic Parser Usage group.add_argument("-q", "--quiet", action="store_true") parser.add_argument("posarg", help="positional argument", type=str) - app = QtGui.QApplication(sys.argv) + app = QtWidgets.QApplication(sys.argv) a = argparseui.ArgparseUi(parser) a.show() app.exec_() @@ -76,7 +76,7 @@ Example using save/load button and keeping the dialog open when pressing ok import argparse import sys - from PyQt4 import QtGui + from PyQt5 import QtWidgets, QtGui import argparseui def do_something(argparseuiinstance): @@ -87,8 +87,8 @@ Example using save/load button and keeping the dialog open when pressing ok parser.add_argument("-m", "--make-argument-true", help="optional boolean argument", action="store_true") parser.add_argument("-o","--make-other-argument-true", help="optional boolean argument 2", action="store_true", default=True) - app = QtGui.QApplication(sys.argv) - a = argparseui.ArgparseUi(parser,use_save_load_button=True,ok_button_handler=do_something) + app = QtWidgets.QApplication(sys.argv) + a = argparseui.ArgparseUi(parser,use_save_load_button=True,ok_button_handler=do_something) a.show() app.exec_() if a.result() != 1: @@ -139,3 +139,4 @@ The following people have contributed to argparseui * Stefaan Himpe * Thomas Hisch + * Thomas Haederle (PyQt5) diff --git a/argparseui/ui.py b/argparseui/ui.py index 2c2b17c..98ca2cd 100644 --- a/argparseui/ui.py +++ b/argparseui/ui.py @@ -18,9 +18,9 @@ import textwrap import argparse -from PyQt4 import QtCore, QtGui +from PyQt5 import QtCore, QtGui, QtWidgets -__VERSION__ = "0.0.4" +__VERSION__ = "0.0.5" def comb(str1, str2): return str1 + str2 @@ -62,7 +62,7 @@ def quote(s): 'unicode' : ' or more unicode strings' } -class ArgparseUi(QtGui.QDialog): +class ArgparseUi(QtWidgets.QDialog): def __init__(self, parser, use_scrollbars=False, remove_defaults_from_helptext=False, helptext_default=' [default=%(default)s]', use_save_load_button=False, window_title="Make your choice", left_label_alignment=None, ok_button_handler=None, cancel_button_handler=None, parent=None): @@ -79,25 +79,25 @@ def __init__(self, parser, use_scrollbars=False, remove_defaults_from_helptext=F self.filename = None self.destToWidget = {} - self.mainLayout = QtGui.QVBoxLayout(self) + self.mainLayout = QtWidgets.QVBoxLayout(self) self.setLayout(self.mainLayout) - self.description = QtGui.QWidget(self) - self.descriptionLayout = QtGui.QVBoxLayout(self.description) + self.description = QtWidgets.QWidget(self) + self.descriptionLayout = QtWidgets.QVBoxLayout(self.description) self.description.setLayout(self.descriptionLayout) - self.options = QtGui.QWidget(self) - self.optionsLayout = QtGui.QFormLayout(self.options) + self.options = QtWidgets.QWidget(self) + self.optionsLayout = QtWidgets.QFormLayout(self.options) if left_label_alignment is not None: self.optionsLayout.setLabelAlignment(QtCore.Qt.AlignLeft if left_label_alignment else QtCore.Qt.AlignRight) self.options.setLayout(self.optionsLayout) - self.buttons = QtGui.QWidget(self) - self.buttonsLayout = QtGui.QHBoxLayout(self.buttons) + self.buttons = QtWidgets.QWidget(self) + self.buttonsLayout = QtWidgets.QHBoxLayout(self.buttons) self.buttons.setLayout(self.buttonsLayout) - self.epilog = QtGui.QWidget(self) - self.epilogLayout = QtGui.QVBoxLayout(self.epilog) + self.epilog = QtWidgets.QWidget(self) + self.epilogLayout = QtWidgets.QVBoxLayout(self.epilog) self.epilog.setLayout(self.epilogLayout) if self.use_save_load_button: @@ -110,7 +110,7 @@ def __init__(self, parser, use_scrollbars=False, remove_defaults_from_helptext=F self.SaveButton = self.addButton("Save options") self.SaveAsButton = self.addButton("Save options as") - self.buttonsLayout.addSpacerItem(QtGui.QSpacerItem(20, 1, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)) + self.buttonsLayout.addSpacerItem(QtWidgets.QSpacerItem(20, 1, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)) self.OkButton.clicked.connect(self.onOk) self.CancelButton.clicked.connect(self.onCancel) @@ -123,10 +123,10 @@ def __init__(self, parser, use_scrollbars=False, remove_defaults_from_helptext=F self.mainLayout.addWidget(self.description) if self.use_scrollbars: - self.scrollableArea = QtGui.QScrollArea(self) + self.scrollableArea = QtWidgets.QScrollArea(self) self.scrollableArea.setWidgetResizable(True) self.scrollableArea.setEnabled(True) - self.scrollableArea.setFrameShape(QtGui.QFrame.NoFrame) + self.scrollableArea.setFrameShape(QtWidgets.QFrame.NoFrame) self.scrollableArea.setWidget(self.options) self.mainLayout.addWidget(self.scrollableArea) self.resize(self.sizeHint()) @@ -138,8 +138,8 @@ def __init__(self, parser, use_scrollbars=False, remove_defaults_from_helptext=F self.mainLayout.addWidget(self.buttons) def addButton(self, label): - self.buttonsLayout.addSpacerItem(QtGui.QSpacerItem(20, 1, QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Minimum)) - b = QtGui.QPushButton(label, self.buttons) + self.buttonsLayout.addSpacerItem(QtWidgets.QSpacerItem(20, 1, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)) + b = QtWidgets.QPushButton(label, self.buttons) self.buttonsLayout.addWidget(b) return b @@ -183,7 +183,7 @@ def create_ui(self): print ("Unsupported type: {0}\n".format(a)) - self.addEpilog() + #self.addEpilog() def addDescription(self): """ @@ -193,7 +193,7 @@ def addDescription(self): descr = self.parser.description else: descr = "Choose which options to include, and define their value below" - label = QtGui.QLabel(descr) + label = QtWidgets.QLabel(descr) self.descriptionLayout.addWidget(label) def addEpilog(self): @@ -204,7 +204,7 @@ def addEpilog(self): descr = self.parser.epilog else: descr = "This options dialog is auto-generated by argparseui {0}!".format(__VERSION__) - label = QtGui.QLabel(descr) + label = QtWidgets.QLabel(descr) self.epilogLayout.addWidget(label) def makeHelpString(self, a): @@ -337,8 +337,8 @@ def makeStoreConstEntry(self, a, optional=True): (represented as a label) """ helpstring = self.makeHelpString(a) - rhslabel = QtGui.QLabel("", self.options) - include = QtGui.QCheckBox(helpstring, self.options) + rhslabel = QtWidgets.QLabel("", self.options) + include = QtWidgets.QCheckBox(helpstring, self.options) if a.default: include.setChecked(True) self.optionsLayout.addRow(include, rhslabel) @@ -353,7 +353,7 @@ def createFunctionToMakeStoreConstEntryCommandLine(self, include_widget, a): while building the ui) """ def to_command_line(): - if type(include_widget) == QtGui.QCheckBox: + if type(include_widget) == QtWidgets.QCheckBox: checked = include_widget.isChecked() else: checked = True @@ -373,42 +373,42 @@ def makeStoreActionEntry(self, a, optional=True): typehelp = self.makeTypeHelp(a) validator = self.getValidator(a) if a.choices: - combobox = QtGui.QComboBox(self.options) + combobox = QtWidgets.QComboBox(self.options) for c in a.choices: combobox.addItem("{0}".format(c)) if a.default is not None: combobox.setCurrentIndex(combobox.findText("{0}".format(a.default))) if optional: - include = QtGui.QCheckBox(comb(helpstring, typehelp), self.options) + include = QtWidgets.QCheckBox(comb(helpstring, typehelp), self.options) enabled = a.default is not None include.setChecked(enabled) self.disableOnClick(combobox)(enabled) include.clicked.connect(self.disableOnClick(combobox)) self.registerDisplayStateInfo(a.dest, [include]) else: - include = QtGui.QLabel(comb(helpstring, typehelp), self.options) + include = QtWidgets.QLabel(comb(helpstring, typehelp), self.options) self.registerDisplayStateInfo(a.dest, [combobox]) self.commandLineArgumentCreators.append(self.createFunctionToMakeStoreEntryCommandLine(include, combobox, a)) self.optionsLayout.addRow(include, combobox) else: - lineedit = QtGui.QLineEdit(self.options) + lineedit = QtWidgets.QLineEdit(self.options) if a.default is not None: lineedit.setText("{0}".format(a.default)) if validator is not None: lineedit.setValidator(validator(self)) if optional: - include = QtGui.QCheckBox(comb(helpstring, typehelp), self.options) + include = QtWidgets.QCheckBox(comb(helpstring, typehelp), self.options) enabled = a.default is not None include.setChecked(enabled) self.disableOnClick(lineedit)(enabled) include.clicked.connect(self.disableOnClick(lineedit)) self.registerDisplayStateInfo(a.dest, [include]) else: - include = QtGui.QLabel(comb(helpstring, typehelp), self.options) + include = QtWidgets.QLabel(comb(helpstring, typehelp), self.options) self.registerDisplayStateInfo(a.dest, [lineedit]) self.commandLineArgumentCreators.append(self.createFunctionToMakeStoreEntryCommandLine(include, lineedit, a)) @@ -421,12 +421,12 @@ def createFunctionToMakeStoreEntryCommandLine(self, include_widget, value_widget while building the ui) """ def to_command_line(): - if type(include_widget) == QtGui.QCheckBox: + if type(include_widget) == QtWidgets.QCheckBox: checked = include_widget.isChecked() else: checked = True if checked: - if type(value_widget) == QtGui.QLineEdit: + if type(value_widget) == QtWidgets.QLineEdit: if argument.option_strings: if argument.nargs == "1" or argument.nargs is None: return ["{0}".format(argument.option_strings[0]), @@ -437,7 +437,7 @@ def to_command_line(): return cmd else: return ["{0}".format(value_widget.text())] - elif type(value_widget) == QtGui.QComboBox: + elif type(value_widget) == QtWidgets.QComboBox: if argument.option_strings: return ["{0}".format(argument.option_strings[0]), "{0}".format(value_widget.currentText())] @@ -455,18 +455,18 @@ def makeCountActionEntry(self, a, optional=True): (represented by a spinbox) """ helpstring = self.makeHelpString(a) - spinbox = QtGui.QSpinBox(self.options) + spinbox = QtWidgets.QSpinBox(self.options) spinbox.setRange(0, 100) spinbox.setValue(a.nargs) if optional: - include = QtGui.QCheckBox(helpstring, self.options) + include = QtWidgets.QCheckBox(helpstring, self.options) enabled = a.default is not None include.setChecked(enabled) self.disableOnClick(spinbox)(enabled) include.clicked.connect(self.disableOnClick(spinbox)) self.registerDisplayStateInfo(a.dest, [include]) else: - include = QtGui.QLabel(helpstring, self.options) + include = QtWidgets.QLabel(helpstring, self.options) self.registerDisplayStateInfo(a.dest, [spinbox]) self.commandLineArgumentCreators.append(self.createFunctionToMakeCountActionCommandLine(include, spinbox, a)) @@ -480,12 +480,12 @@ def createFunctionToMakeCountActionCommandLine(self, include_widget, value_widge """ def to_command_line(): - if type(include_widget) == QtGui.QCheckBox: + if type(include_widget) == QtWidgets.QCheckBox: checked = include_widget.isChecked() else: checked = True if checked: - if type(value_widget) == QtGui.QSpinBox: + if type(value_widget) == QtWidgets.QSpinBox: if argument.option_strings: return [("{0}".format(argument.option_strings[0]))]*value_widget.value() else: @@ -511,8 +511,8 @@ def cleanup(row, col): data.append(cellText) tablewidget.setColumnCount(len(data)+1) for c, d in enumerate(data): - tablewidget.setItem(0, c, QtGui.QTableWidgetItem(d)) - tablewidget.setItem(0, len(data), QtGui.QTableWidgetItem("")) + tablewidget.setItem(0, c, QtWidgets.QTableWidgetItem(d)) + tablewidget.setItem(0, len(data), QtWidgets.QTableWidgetItem("")) tablewidget.blockSignals(False) return cleanup @@ -521,8 +521,8 @@ def makeAppendActionEntry(self, a, optional=True): creates a table widget for appending arguments into a list """ helpstring = self.makeHelpString(a) - tablewidget = QtGui.QTableWidget(1, 1, self.options) - sizePolicy = QtGui.QSizePolicy(QtGui.QSizePolicy.Expanding, QtGui.QSizePolicy.Expanding) + tablewidget = QtWidgets.QTableWidget(1, 1, self.options) + sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Expanding) sizePolicy.setHorizontalStretch(1) sizePolicy.setVerticalStretch(1) sizePolicy.setHeightForWidth(tablewidget.sizePolicy().hasHeightForWidth()) @@ -539,17 +539,17 @@ def makeAppendActionEntry(self, a, optional=True): for i in range(needed-cnt+1): tablewidget.insertColumn(tablewidget.columnCount()) for column, d in enumerate(a.default): - tablewidget.setItem(0, column, QtGui.QTableWidgetItem(d)) + tablewidget.setItem(0, column, QtWidgets.QTableWidgetItem(d)) if optional: - include = QtGui.QCheckBox(helpstring, self.options) + include = QtWidgets.QCheckBox(helpstring, self.options) enabled = a.default is not None include.setChecked(enabled) self.disableOnClick(tablewidget)(enabled) include.clicked.connect(self.disableOnClick(tablewidget)) self.registerDisplayStateInfo(a.dest, [include]) else: - include = QtGui.QLabel(helpstring, self.options) + include = QtWidgets.QLabel(helpstring, self.options) self.registerDisplayStateInfo(a.dest, [tablewidget]) self.commandLineArgumentCreators.append(self.createFunctionToMakeAppendCommandLine(include, tablewidget, a)) @@ -563,12 +563,12 @@ def createFunctionToMakeAppendCommandLine(self, include_widget, tablewidget, arg """ def to_command_line(): data = [] - if type(include_widget) == QtGui.QCheckBox: + if type(include_widget) == QtWidgets.QCheckBox: checked = include_widget.isChecked() else: checked = True if checked: - if type(tablewidget) == QtGui.QTableWidget: + if type(tablewidget) == QtWidgets.QTableWidget: if argument.option_strings: for c in range(tablewidget.columnCount()): cellText = "{0}".format(tablewidget.item(0, c).text() if tablewidget.item(0, c) else "") @@ -632,7 +632,7 @@ def onOk(self): self.ok_button_handler(self) else: mutexes = "\n".join([",".join(o) for o in offensive_options]) - QtGui.QMessageBox.question(self, 'Validation error', "The following options are mutually exclusive:\n{0}".format(mutexes), QtGui.QMessageBox.Ok, QtGui.QMessageBox.Ok) + QtWidgets.QMessageBox.question(self, 'Validation error', "The following options are mutually exclusive:\n{0}".format(mutexes), QtWidgets.QMessageBox.Ok, QtWidgets.QMessageBox.Ok) def onCancel(self): """ @@ -653,18 +653,18 @@ def resetAllWidgets(self, argparser): # make sure all checkboxes that were already enabled are disabled first for a in self.destToWidget: for w in self.destToWidget[a]: - if type(w) == QtGui.QCheckBox: + if type(w) == QtWidgets.QCheckBox: w.setChecked(False) w.clicked.emit(False) - elif type(w) == QtGui.QTableWidget: + elif type(w) == QtWidgets.QTableWidget: w.setColumnCount(1) - w.setItem(0, 0, QtGui.QTableWidgetItem("")) + w.setItem(0, 0, QtWidgets.QTableWidgetItem("")) def onLoad(self): """ handle load button pressed """ - filename = QtGui.QFileDialog.getOpenFileName() + filename = QtWidgets.QFileDialog.getOpenFileName() if filename: helper = argparse.ArgumentParser(add_help=False, parents=[self.parser], fromfile_prefix_chars='@') self.resetAllWidgets(helper) @@ -684,7 +684,7 @@ def copyActionValuesToUi(self, a, result): if a.dest in self.destToWidget: for w in self.destToWidget[a.dest]: data = result.__getattribute__(a.dest) - if type(w) == QtGui.QCheckBox: + if type(w) == QtWidgets.QCheckBox: if data is not None: if type(data) == bool: w.setChecked(data) @@ -692,22 +692,22 @@ def copyActionValuesToUi(self, a, result): else: w.setChecked(True) w.clicked.emit(True) - elif type(w) == QtGui.QLineEdit: + elif type(w) == QtWidgets.QLineEdit: if data is not None: if type(data) == type([]): w.setText(" ".join(["{0}".format(d) for d in data])) else: w.setText("{0}".format(data)) - elif type(w) == QtGui.QSpinBox: + elif type(w) == QtWidgets.QSpinBox: if data is not None: w.setValue(int("{0}".format(data))) - elif type(w) == QtGui.QComboBox: + elif type(w) == QtWidgets.QComboBox: if data is not None: w.setCurrentIndex(w.findText("{0}".format(data))) - elif type(w) == QtGui.QTableWidget: + elif type(w) == QtWidgets.QTableWidget: #clear existing defaults first w.setColumnCount(1) - w.setItem(0, 0, QtGui.QTableWidgetItem("")) + w.setItem(0, 0, QtWidgets.QTableWidgetItem("")) cnt = w.columnCount() if data is not None: needed = len(data) @@ -715,7 +715,7 @@ def copyActionValuesToUi(self, a, result): for i in range(needed-cnt+1): w.insertColumn(w.columnCount()) for column, d in enumerate(data): - w.setItem(0, int("{0}".format(column)), QtGui.QTableWidgetItem(d)) + w.setItem(0, int("{0}".format(column)), QtWidgets.QTableWidgetItem(d)) def onSave(self): """ @@ -731,7 +731,7 @@ def onSave(self): f.write(line+"\n") except IOError: import os.path - QtGui.QMessageBox.critical(self, + QtWidgets.QMessageBox.critical(self, "Critical", "Couldn't write to file {0}".format(os.path.abspath(self.filename))) @@ -740,7 +740,7 @@ def onSaveAs(self): """ what to do when the save as button is clicked """ - filename = QtGui.QFileDialog.getSaveFileName() + filename = QtWidgets.QFileDialog.getSaveFileName() if filename: self.filename = filename self.onSave() @@ -754,7 +754,7 @@ def parse_args(self): if __name__ == "__main__": import sys - from PyQt4 import QtGui + from PyQt5 import QtWidgets # EXPERIMENT USING BASIC PARSER parser = argparse.ArgumentParser() @@ -770,7 +770,7 @@ def parse_args(self): group.add_argument("-q", "--quiet", action="store_true") parser.add_argument('--foo', type=int, nargs='+') parser.add_argument('--bar', type=int, nargs=2, metavar=('bar', 'baz')) - app = QtGui.QApplication(sys.argv) + app = QtWidgets.QApplication(sys.argv) a = ArgparseUi(parser, left_label_alignment=True, use_scrollbars=True, use_save_load_button=True) a.show() app.exec_()