Text tag events

Hi, I’m trying to create a text tag that changes the key press behavior. According to the Text widget conceptual overview, this is possible:

They [tags] can instead affect the behavior of mouse and key presses, “lock” a range of text so the user can’t edit it, or countless other things.

I think I should connect to the event signal of the tag. This works for button presses, but not for key presses. What am I doing wrong? Example code:

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

def link_event_cb(tag, object_, event, iter_):
    if event.get_event_type() == Gdk.EventType.BUTTON_PRESS:
        print("Link pressed") # This does work 
    if event.get_event_type() == Gdk.EventType.KEY_RELEASE:
        print("Button pressed while cursor in link") # This does not work
    return False

win = Gtk.Window()
win.connect("destroy", Gtk.main_quit)

# Create textview and add a text to it
view = Gtk.TextView()
buf = view.get_buffer()
buf.set_text("This is my long text with a link in it.")

# Create a tag and apply it
link = buf.create_tag( None, foreground = "#FF0000")
start_it = buf.get_iter_at_offset(28)
end_it = buf.get_iter_at_offset(32)

# Connect the event signal to the tag


Thanks for the help!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.