Richard Jones' Log: Something I'm working on...

with gui.column(): text = gui.label('hello!') items = gui.selection(['one', 'two', 'three']) with gui.button('click me!'): def on_click(): text.value = items.value text.foreground = red
How does the gui get a reference to on_click?
Nevermind - I assume it's harvesting locals()?
I don't code Python much, but this looks really interesting :)
Alright, I'm stumped. How the heck did you get the context manager to find the function defined within?
I've thought it should be simple ever since I saw the spec, and am frustrated that it's not. How hacky is your solution?
It's just the right amount of hacky :)
This kind of makes python look a bit like javascript.
except rather than object attributes you are using scope... is that allowed? or will the programming gods smite you?
is on_click a defined function i.e can I do an 'on_mouseover(), on_mousedown(), on_mouseout()?
After thinking about this a bit, I'm unconvinced of it's usefulness. If you translate 'with' -> 'class', you more-or-less have a declarative class definition but without the benefits of python's object/class-model (inheritance, for example).
This is pretty clean and nice. I've posted a similar short snippet that has it's magic performed by the traits library here.
Istead of:
with gui.button('click me!'): def on_click(): ...why not:
@gui.button('click me!') def button(): ...Advantages --
- no deep introspection
- binds to local variable button
- call to gui.button()() is similar to gui.label()
- it's clear there is only one event.
@ilya, you're reading ahead - the decorator form is already coded :)
As for the event name though ... hmm.
OK, now I'm impressed. That's the most unexpected use for contextmanagers I've yet seen. neat.