I am using GTK 4 with Python and have a Gtk.Entry
widget which I would like to limit the characters which can be entered to numbers only. How do I do that?
You could connect the changed
signal of teh Gtk.Entry
to a callback function. In the call back function check the content of the input and remove ny non-numeric chars
def on_entry_changed(self, entry):
# Use GLib.idle_add to delay the action until the main loop is idle
GLib.idle_add(filter_entry_text, entry)
def filter_entry_text(entry):
text = entry.get_text()
new_text = ''.join([char for char in text if char.isdigit()])
if new_text != text:
entry.set_text(new_text)
entry.set_position(-1)
return False