rawpython / remi

Python REMote Interface library. Platform independent. In about 100 Kbytes, perfect for your diet.
Apache License 2.0
3.48k stars 401 forks source link

Could event handler be connected at initialization? #437

Closed waingt closed 3 years ago

waingt commented 3 years ago

This GUI library is so cool. But, when I create a button, I must assign a variable to it at first, then connect event and append it to container. (this takes 3 lines) I suppose it is cooler if this could be done in one line

for example:

class Button(gui.Widget, gui._MixinTextualWidget):
    def __init__(self, text="", onclick=None, *args, **kwargs):
        super(Button, self).__init__(*args, **kwargs)
        self.type = "button"
        self.set_text(text)
        if type(onclick) == list:
            if len(onclick) == 2:
                self.onclick.do(onclick[0], *onclick[1])
            elif len(onclick) == 3:
                self.onclick.do(onclick[0], *onclick[1], **onclick[2])
        elif onclick:
            self.onclick.do(onclick)

usage:

container.append(Button("click", onclick=[self.btnclick, ["somedata"]]))
dddomodossola commented 3 years ago

@waingt This is really cool, I will implement it in a near future! Thank you for the suggestion ;-) However I will make a more generic implementation to avoid named parameters like "onclick". I suppose to accept a parameter like Button("my button", do={Button.onclick: my_onclick_handler, Button.onmouseover: my_onmouseover_handler}) .