slint icon indicating copy to clipboard operation
slint copied to clipboard

Create Pythonic API to load Slint components

Open tronical opened this issue 1 year ago • 1 comments

The objective of this task is explore the possibility of providing dynamically generated types for “.slint” files. Beyond classic objects with properties, we can experiment with dynamic imports like in the popular “sh” module. We should review this API with experienced Pythonistas in public discussions.

This work will be sponsored by the NLnet foundation.

Edit: The API review makes most sense after the documentation is up and the rest of the project is ready for review, so let's exclude that from the scope of this issue and include it in the before the final release.

tronical avatar Dec 11 '23 09:12 tronical

The initial load_file function is added, which exposes instances as objects with properties and callbacks (as on-prefixed properties). The dynamic import is missing still.

tronical avatar Feb 27 '24 08:02 tronical

One way for the dynamic import would be to allow for writing an __init__.py stub that associates a directory with a specific .slint file, and then allow for writing:

Support ui/__init__.py referenced ui/main_window.slint:

from ui import MainWindow

window = MainWindow()
window.show();

tronical avatar Mar 05 '24 14:03 tronical

Without __init__.py a neater way might be to add a finder/loader to sys.meta_path and support import foo, which loads foo.slint.

Example:

foo.slint:

export component MainWindow { ... }

blah.py:

import slint
import foo

main_window = foo.MainWindow();
main_window.run()

(this would also work of course with import foo as bar or import MainWindow from foo)

tronical avatar Mar 06 '24 11:03 tronical

I've updated the description with a summary of the implemented API. Closing this as the initial port being done. Documentation is still missing, but that's part of a milestone that was agreed on separately.

tronical avatar Mar 07 '24 11:03 tronical