Is it possible to define closure from Python code?

I want to use the bind expession in Blueprint file as this example:

label: bind $format_bytes(template.file-size) as <string>

But I don’t know how to define external closure in Python. Is it possible, because the Gtk.Template only provide Child and Callback.

You use @Gtk.Template.Callback() to define the closure.

Thanks, I can define the closure now, but I find the corresponding Python function is weird in terms of its parameter.
Take this source as example.

In the window.blp file, I have:

ToggleButton toggle_scanner {
  icon-name: 'seeing-symbolic';
  tooltip-text: _('Scanner');
  active: bind $in_scanner_mode(job_viewstack.visible-child-name) as <bool>;
  toggled => $switch_to_scanner();
}

and I have to define in_scanner_mode in Python like this:

@Gtk.Template(resource_path='/vn/hoabinh/quan/CoBangB/ui/window.ui')
class CobangbWindow(Adw.ApplicationWindow):
    ...

    @Gtk.Template.Callback()
    def in_scanner_mode(self, window: Self, child_name: str) -> bool:
        return child_name == JobName.SCANNER

The unusual part is that, self is passed an object of Child type, and the window is passed as 2nd parameter.

What I expect is the in_scanner_mode should be defined like this:

@Gtk.Template.Callback()
def in_scanner_mode(self, child_name: str) -> bool:

Is this behavior designed, or it may change in the future?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.