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?
Pluggable Python program
976 Views Asked by mtk358 AtThere are 2 best solutions below
dash-tom-bang
On
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).
Related Questions in PYTHON
- How to store a date/time in sqlite (or something similar to a date)
- Instagrapi recently showing HTTPError and UnknownError
- How to Retrieve Data from an MySQL Database and Display it in a GUI?
- How to create a regular expression to partition a string that terminates in either ": 45" or ",", without the ": "
- Python Geopandas unable to convert latitude longitude to points
- Influence of Unused FFN on Model Accuracy in PyTorch
- Seeking Python Libraries for Removing Extraneous Characters and Spaces in Text
- Writes to child subprocess.Popen.stdin don't work from within process group?
- Conda has two different python binarys (python and python3) with the same version for a single environment. Why?
- Problem with add new attribute in table with BOTO3 on python
- Can't install packages in python conda environment
- Setting diagonal of a matrix to zero
- List of numbers converted to list of strings to iterate over it. But receiving TypeError messages
- Basic Python Question: Shortening If Statements
- Python and regex, can't understand why some words are left out of the match
Related Questions in QT
- qt c++ fonction converting adress to coordinates (longitude, latitude)
- Qml table and chart using python
- Qt: running callback in the main thread from the worker thread
- i have installed qt version 6.0.3 and this error QMYSQL driver not loaded displaying again and again
- Frameless Qt + WinAPI maximized window size is bigger than the availableGeometry()
- new window with c++ qt
- How to get scaling from transformation matrix
- How to build just Qt core libraries from Qt sources
- doxyqml not documenting qml files properly
- Incorrect assignment from a QStringList to a char * array
- How to make QT Chart size larger than widget size?
- Queued async operations with QtConcurrent interfere QImage from freed
- Questions about qt5 dynamic link library
- how to document QML files inside C++ project?
- How do I keep my screen contents centered and also have a scrollbar in QT?
Related Questions in PLUGINS
- Chrome/Firefox plug-in for automatically opening the dev tools and executing code in the console
- Creating a modal window in product edit page in Shopware6 and saving data to custom table(repository) from a form within the modal window
- Swiper.js moves slides outside of viewport/page
- Responsive Lightbox & Gallery issue
- Unity - How to add C++ file directly to be used as a Plug-in
- Can not switch camera while recording with camera plugin, setDescription working but preview doesn't change
- can anyone help me for NSIS Script I have a case where I need to check cases can be 3.3 3.42 23.5 23.56w 3.467 999.999 999.99a but not 1.b or 8..a
- How can I know if a vscode intelliSense suggestion is visible?
- Loader / Spinner infinite | Filter Everything Pro
- HTML Not Rendering and Displaying as Plain Text on Frontend with Ninja Forms in WordPress
- I want to run my own interface that I coded with Python in RoboDK
- How to redirect uploads from customers to go into specific folder?
- TestNG update related issue
- Co Author Plus: Need to add co authors names with link
- JS-issue "undefined query" when trying to create chrome-plugin
Related Questions in PYQT4
- No showTearOffMenu() in PyQt4 QMenu
- how to install Pyqt4 currently on raspberry pi 3 in python 2.7?
- GroupedDragging in Qt4
- How to install PyQt4 in 2024 on Ubuntu (to run an old project)?
- AttributeError: 'NoneType' object has no attribute 'registerObject' inPyQt
- How to Browse Data file and extract data
- SWIG Interface for calling Qt from Python
- How to programatically move a QDockWidget from one dock area to another?
- PyQT- TypeError: connect() failed between triggered(bool) and on_open()
- Error with qt.qpa.xcb and qt.qpa.plugin when trying to install gpt4all (https://gpt4all.io/index.html)
- PyQt: Is there a way to hide a column in QTreeWidgetItem?
- Pipe output from Process into GUI
- ImportError: DLL load failed while importing QtGui: The specified module could not be found
- PYQT5 Open menu on cursor position in QTextEditor
- PyQt table row background colour based on cell value with SQlite database
Related Questions in PLUGGABLE
- Passing an association to a pluggable widget
- How do i connect to different oracle databases that are installed in a same Linux server
- Connecting to Oracle Database in SQL Developer shows only my pluggable databases and NOT the template - PDB$SEED
- Get product id in "woocommerce_quantity_input" pluggable function
- Hyper Ledger Fabric Pluggable Consensus
- How to create a simple Java + Maven pluggable application?
- Why pluggable db user became administrator?
- how to connect to pluggable database when there are two instances
- How to plugin siddhi web editor within existing project?
- How to implement Pluggable Adapter design pattern in Java
- How to hook in wp_set_password WordPress function with WooCommerce?
- Custom Asynchronous Pluggable Protocols opening app multiple times
- Wordpress Error 500 after migration
- Unable to get to wp-admin log in page
- How to build pluggable dashboard using Angular 4?
Trending Questions
- UIImageView Frame Doesn't Reflect Constraints
- Is it possible to use adb commands to click on a view by finding its ID?
- How to create a new web character symbol recognizable by html/javascript?
- Why isn't my CSS3 animation smooth in Google Chrome (but very smooth on other browsers)?
- Heap Gives Page Fault
- Connect ffmpeg to Visual Studio 2008
- Both Object- and ValueAnimator jumps when Duration is set above API LvL 24
- How to avoid default initialization of objects in std::vector?
- second argument of the command line arguments in a format other than char** argv or char* argv[]
- How to improve efficiency of algorithm which generates next lexicographic permutation?
- Navigating to the another actvity app getting crash in android
- How to read the particular message format in android and store in sqlite database?
- Resetting inventory status after order is cancelled
- Efficiently compute powers of X in SSE/AVX
- Insert into an external database using ajax and php : POST 500 (Internal Server Error)
Popular # Hahtags
Popular Questions
- How do I undo the most recent local commits in Git?
- How can I remove a specific item from an array in JavaScript?
- How do I delete a Git branch locally and remotely?
- Find all files containing a specific text (string) on Linux?
- How do I revert a Git repository to a previous commit?
- How do I create an HTML button that acts like a link?
- How do I check out a remote Git branch?
- How do I force "git pull" to overwrite local files?
- How do I list all files of a directory?
- How to check whether a string contains a substring in JavaScript?
- How do I redirect to another webpage?
- How can I iterate over rows in a Pandas DataFrame?
- How do I convert a String to an int in Java?
- Does Python have a string 'contains' substring method?
- How do I check if a string contains a specific word?
First, use a QFileDialog or more conveniently the static function QFileDialog.getOpenFileName to let the user pick the
.pyfile 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 variablePYTHONPATH), 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 (assumingfilepathis theQStringwith the path to the file, and that you're using a filesystem/platform with natively Unicode filenames -- otherwise you'll have to add whatever.encodeyour platform and filesystem require)...: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
importfromabove), I suggest: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.