robotools / vanilla

A Pythonic wrapper around Cocoa.
MIT License
78 stars 28 forks source link

Callbacks in *StackView stop working when added through loop #165

Closed eweracs closed 2 years ago

eweracs commented 2 years ago

In this example, the buttons will stop working.

from vanilla import Button, VerticalStackView, Window

class Demo:
    def __init__(self):
        self.w = Window((1, 1))

        metrics = {
            "margin": 10
        }

        for number in ["one"]:
            group = Group("auto")
            button = Button("auto", "Button", callback=self.button_callback)

            group.horizontalStack = VerticalStackView("auto",
                views=[
                    dict(view=button),
                ],
                spacing=10,
                edgeInsets=(10, 10, 10, 10),
            )

            group_rules = [
                "H:|-margin-[horizontalStack]-margin-|",
                "V:|-margin-[horizontalStack]-margin-|",
            ]

            group.addAutoPosSizeRules(group_rules, metrics)

            setattr(self.w, number, group)

        rules = [
            "H:|[one]|",
            "V:|[one]|",
        ]

        self.w.addAutoPosSizeRules(rules, metrics)

        self.w.open()

    def button_callback(self, sender):
        print(sender)

Demo()

This is not an issue with adding the stack view to the group, doing so without a loop works fine.

eweracs commented 2 years ago

Fix found: the buttons need to be stored somewhere, adding them to something like self.button_list works.