0

I need to implement a input dialog box that takes in numerical values. How is it done in Pygobject?. It is similar to excel taking numerical inputs.

arvind
  • 49
  • 6
  • I was going to suggest to override the do_input_text virtual method and I was writing you a sample, unfortunately I cannot make it work, so I reported a bug. I don't really see another method to do that – gianmt Apr 30 '20 at 18:13

2 Answers2

0

If you're looking for simple numerical values, you can use Gtk.SpinButton.

The more general way getting input from the user in the UI is Gtk.Entry. That widget does not support built-in validation, but it can be trivially implemented, similarly as shown in this stackoverflow answer (by overriding the insert_text method).

If you want to show validation errors to your user, you can use either CSS styling, or for example set the secondary icon (with e.g. the secondary_icon_name property) to a warning sign, with an accompanying label explaining what is wrong.

nielsdg
  • 2,318
  • 1
  • 13
  • 22
0

With some help from Ricardo Silva Veloso, this is basically the code I was suggesting to write in my first comment (also considering @nielsdg secondary icon suggestion):

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk

class MyEntry(Gtk.Entry, Gtk.Editable):
    __gtype_name__ = 'MyEntry'
    
    def __init__(self, **kwargs):
        super(MyEntry, self).__init__(**kwargs)

    def do_insert_text(self, text, length, position):
        if text.isnumeric():
            self.props.secondary_icon_name = None
            self.get_buffer().insert_text(position, text, length)
            return length + position
        else:
            self.props.secondary_icon_name = "dialog-error"
            self.props.secondary_icon_tooltip_text = "Only numbers are allowed"
            return position

win = Gtk.Window()
myentry = MyEntry()

win.add(myentry)

win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()
gianmt
  • 1,162
  • 8
  • 15