Search code examples
pythonqtpluginspyqt4pluggable

Pluggable Python program


I want to make a PyQt4 program that supports plugins. Basically I want the user to be able to write QWidget subclasses in PyQt4 and add/remove them from the main application window via a GUI. How would I do that, especially the plugin mechanism?


Solution

  • First, use a QFileDialog or more conveniently the static function QFileDialog.getOpenFileName to let the user pick the .py file they want to "plug into" your GUI.

    Next, if you want to allow importing the plugin from anywhere on the disk (as opposed to, from specific directories previously added to your sys.path, e.g. via the environment variable PYTHONPATH), you can "do it right" (a non-negligible amount of work), via the imp module in the standard Python library), or you can "cut corners" as follows (assuming filepath is the QString with the path to the file, and that you're using a filesystem/platform with natively Unicode filenames -- otherwise you'll have to add whatever .encode your platform and filesystem require)...:

    import sys, os
    
    def importfrom(filepath):
        ufp = unicode(filepath)
        thedir, thefile = os.path.split(ufp)
        if sys.path[0] != thedir:
          sys.path.insert(0, thedir)
        themodule, theext = os.path.splitext(thefile)
        return __import__(themodule)
    

    This isn't thread-safe because it may have a side effect on sys.path, which is why I said it's "cutting corners"... but then, making thread-safe imports (and "clean" imports from "anywhere") is really hard work (probably worth a separate question if you need to, since it really has nothing much to do with the gist of this one, or PyQt, &c).

    Once you do have the module object (the result from importfrom above), I suggest:

    from PyQt4 import QtGui 
    import inspect
    
    def is_widget(w):
        return inspect.isclass(w) and issubclass(w, QtGui.QWidget)
    
    def all_widget_classes(amodule):
        return [v for n, v in inspect.getmembers(amodule, is_widget)]
    

    This returns a list with all the widget classes (if any) defined (at the top level) in module amodule.

    What you want to do next is up to you, of course. Perhaps you want to give some kind of error messages if the list is empty (or maybe also if it has more than one item? or else how do decide which widget class to use?) or else instantiate the widget class (how many times? At what coordinates? And so on -- questions only you can answer) and show the resulting widget(s) in the appropriate spot(s) on your window.