Add 'events' to widgets
See original GitHub issueThere are a couple of open PR’s relating to adding events to widgets. #14 and #118 .
Its clearly a good idea, so I have opened this issue to keep track.
A couple of initial points / questions come to mine
-
The implementation of this should be via a mixin, its a shared function of many widgets
-
There is a choice re interface, functions or properties.
tk currently uses functions like on_mouse_click
to set a command, this is familiar to tk users.
other “zero” libraries have moved to using properties to set a command, e.g. gpiozero
def my_func():
print("hi")
button = Button(17)
button.when_pressed = my_func
If implemented as properties (which I think is a good idea) should they be when_mouse_clicked
, when_key_pressed
, when_mouse_moved
- its a bit more verbose, but it is simple to understand.
This also lends its self to implementing other events such as when_pressed
for a PushButton or when_changed
for a TextBox.
- The tk event callbacks will return
Event
objects http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm - is this the right thing to do for guizero?
One of the problems is that the Event
object is a tk object not a guizero object, so (as an example) it refers to the tk widget not the guizero widget.
I think the right approach is to create an Event structure for guizero which returns guizero widgets but could also be extended to include other useful values in the future e.g. “imagine a when_changed
event on a Slider
” would return the Event structure but also the value the Slider changed too.
Issue Analytics
- State:
- Created 6 years ago
- Comments:14 (5 by maintainers)
Top GitHub Comments
I am trying to code a combo box that when changed alters the colour of another text box. The combo box widget really needs a when_changed event. The existing events only track over the un-expanded COMBO box. This is messy as you have to position your mouse OVER the original combo box for mouse events to register AFTER you have actually made a change.
edited to use code block
Just used the standard
command
on the Combo box. You don’t need to use all the custom events.^ I haven’t tested this code, but that is what the Combo command does.