How to place the mouse cursor at a specific position in a text field?

1

I am trying to format a field of type entry of gtk that I am using for the dates entry and wanted to add the character / in certain positions confirm the user will typing ..

And the only thing missing is to position the mouse cursor to the last position of the string when I add the / character. I tried to use the set_position() method but apparently did not work.

    
asked by anonymous 27.08.2015 / 22:11

1 answer

0

I went through the same problem, and I used the following:

campo.set_position(-1)

To facilitate I made an example, where the text cursor will always go to the end.

from gi.repository import Gtk, GObject

class Window(Gtk.Window):
    def __init__(self):
        self.entry = Gtk.Entry()
        self.entry.connect('changed', self.set_cursor)
        Gtk.Window.__init__(self)
        self.add(self.entry)
        self.show_all()
        self.connect('delete-event', Gtk.main_quit)

    def set_cursor(self, widget):
        widget.handler_block_by_func(self.set_cursor) # bloqueia o sinal da entry
        GObject.idle_add(self.entry.set_position, -1) # cursor do texto no final do campo
        widget.handler_unblock_by_func(self.set_cursor) # desbloqueia o sinal da entry


Window()
Gtk.main()
    
28.11.2016 / 21:22