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

Asked

Viewed 649 times

1

I’m trying to format a field like entry gtk I’m using for date entry and wanted to add the character / in certain positions confirm the user will typing..

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

  • You can ask in Portuguese here Gustavo, welcome.

  • haha @re22 thanks, I’ll change the question!

  • 1

    Mouse cursor or text cursor? Note that they are different things.

1 answer

1


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

campo.set_position(-1)

To make it easy 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()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.