Pluggable Python program

前端 未结 2 518
一个人的身影
一个人的身影 2021-02-11 02:38

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 win

2条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-11 03:33

    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).

提交回复
热议问题