I want to base the logic in my app on a finite state machine. Can the event system be used with arbitrary events on arbitrary classes or is it restricted to the components?
I’ve not tried, but I know you can’t trap any other events outside the ones Anvil expose on components (eg I can’t trap mouse events on a label), so my guess would be no.
That said, you’ve piqued my interest. Gimme a few mins…
Ok, I don’t think you can use the built in event system (though it would take Anvil central to say for sure), but you can easily build your own dispatcher and listener system, which I’m in the process of testing. Gimme an hour (yes, I’m bored!) …
Not sure if this is quite what you are looking for :
and here’s a very simple project showing it working. Note I had to disable the python 3 checking as the client only uses python 2-ish, but it seems to work :
https://anvil.works/build#clone:3PLVD3QU7F6Q3VX7=GE7RGUD665IRBZY6KLK75DU6
edit
This one looks interesting as well : https://github.com/etissieres/PyEventEmitter
edit 2
updated 18/07/19 @ 09:28 to show attaching to an arbitrary class.
@simon.m.shapiro - was that any use to you? If not can you explain your use case, I might be able to help more.
I don’t think you can use the built in event system (though it would take Anvil central to say for sure)
The set_event_handler
and raise_event
methods are methods of Forms and components, so that’s correct, you can’t use them for pure Python classes.
@david.wylie That library is a good find - looks like it does the job, works in Skulpt, and only 111 lines of code!
Thanks very much.
I had replied to the forum from my email and just noticed that it bounced. It works perfectly.