0

I want to create a small tool, that will read all the python tools in the folder, put them in a QListWidget and every time I double-click on one of the tools, it will run.

ToolList.py
That's the main window with the tools

import sys
import glob
from PySide import QtGui


class List(QtGui.QListWidget):
    def __init__(self, parent=None):
        super(List, self).__init__(parent)

        # Add tools
        for f in glob.glob("tool_*.py"):
            self.addItem(f)
        self.itemDoubleClicked.connect(self.execute_python)

    def execute_python(self):
        for itm in self.selectedItems():
            execfile(itm.text())


if __name__ == '__main__':
    app = QtGui.QApplication([])
    form = List()
    form.show()
    sys.exit(app.exec_())

tool_test1.py
When I double click this one, it disappears automatically

import sys
from PySide import QtGui

if __name__ == '__main__':
    app = QtGui.QApplication.instance()
    but = QtGui.QPushButton()
    but.show()

tool_test2.py
And when I double click this one, it crashes saying QCoreApplication::exec: The event loop is already running

import sys
from PySide import QtGui

if __name__ == '__main__':
    app = QtGui.QApplication.instance()
    but = QtGui.QPushButton()
    but.show()
    sys.exit(app.exec_())

Any ideas?

Thanks, Nick

Nick
  • 221
  • 2
  • 12
  • Why are you using `execfile`, instead of just importing the modules? – ekhumoro Aug 08 '16 at 16:28
  • My idea was that I want to have the tools updatable. So, even if someone has the "ToolList" open, if I update the tool, when they run it, they will open the updated version. A second reason is that it's very likely that we will end up having hundreds of tools. If I import all these tools as modules just in order to use one tool, isn't it going to be an overkill? – Nick Aug 08 '16 at 22:32
  • 1
    Why would you need to import them all at once? They can be imported dynamically using either the [importlib](http://docs.python.org/2/library/importlib.html#module-importlib) or [imp](http://docs.python.org/2/library/imp.html#module-imp) module, and updated using [reload](https://docs.python.org/2/library/functions.html#reload) (I assume you're still using python2). However, I suggest you do a web-search for "python plugin system" as this is a very well-trodden path. – ekhumoro Aug 08 '16 at 23:21
  • awesome! Many thanks for that. Yep, I'm using 2.7. I won't have the time today to have a look because I was assigned to another task, but I will come back to that and I'll let you know if I managed to do what you said (sorry I'm new to Python). My main question then is, will the Widget open after the import, after the reload or will I need a spefic def to execute? – Nick Aug 09 '16 at 06:56
  • Usually, there will be something like a `start()` function, or perhaps a plugin class with a `start()` method. It's best to avoid having any code that is executed during import. You may also need a `cleanup()` function that can be called before reloading a plugin. – ekhumoro Aug 09 '16 at 19:07

0 Answers0