Andereoo / TkinterWeb

Python bindings for Tkhtml.
MIT License
146 stars 16 forks source link

feature request: onchange handler equivalent #67

Closed nickzoic closed 1 year ago

nickzoic commented 1 year ago

It'd be handy to have a way to hook form field changes, similar to the html <input onchange=""> functionality. This could work very similarly to handle_form_submsision, but be called whenever a field (input or textarea) is changed. The callback could receive the form field name and new value. It's kind of possible to do this by binding Tk events after the form is rendered, but it'd be nicer if it was part of this library.

Andereoo commented 1 year ago

Thanks for the suggestion! You can embed your own widgets in TkinterWeb and have full control over them: https://github.com/Andereoo/TkinterWeb/blob/main/tkinterweb/docs/GEOMETRY.md. Does that suffice, or would you prefer having onchange built in to form elements instead?

nickzoic commented 1 year ago

Hmmm, okay, I hadn't noticed that mechanism at all! I'll have a look ...

What I want to do could probably be done with frame.bind_all() other than the Combobox you're using doesn't generate <<ComboboxSelected>> or equivalent so far as I can tell. This would be an easy fix in combobox-2.3.tm. The FileSelector and ColourSelector widget classes would need something similar too I guess.

nickzoic commented 1 year ago

https://github.com/nickzoic/TkinterWeb/tree/form-change-events <- this idea as a sketchy branch.

Andereoo commented 1 year ago

I just merged your pull request.

The <<Modified>> event was working on and off for me on text fields, so I'm using validatecommand instead for them. It seems to be working great now. I also made the virtual events be emitted by the widgets themselves and not the frame. That way, in your callback, you can use event.widget to get the exact widget that was edited and manipulate it as you wish.

Thanks!