问题
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?
回答1:
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.
回答2:
Have a directory for the plugins, define an interface for those plugins, and walk the directory importing them.
I've never done this in Python, but in C the way I did it was to define a number of functions that the plugin needed to implement. The basic key elements that are necessary is the name of the things in the plugin (so that you can display them in your UI so that the user can instantiate them) and a factory to actually create the things. I suppose in Python it'd be pretty trivial to just do those as one dict that you can return from the module.
E.g. declare that all plugins must author a function called GetPluginInfo() which returns a dictionary of name:class mappings.
Actually now that I think about it you could probably just import the file and look for classes that are subclasses of whatever you care about and not require any explicit API be implemented. I haven't done a lot of that but basically you'd walk the module's dir() and test to see if each thing is a subclass of QWidget (for example).
来源:https://stackoverflow.com/questions/3588915/pluggable-python-program