Search code examples
pythongtkgladegtk3pygobject

Python2/Pygobject/Gtk3/Glade3 check TextView focus


I'm designing a text editor-esque application using Python2.7 and Gtk3, and I'm not very sure on how to set up a handler to check if the main TextView is currently in focus, so I can disable menu items (e.g. Edit -> Copy etc.) accordingly.

In order to create the tabbed text editor, I use a Gtk.Notebook as the main body, and each time File -> New is activated I create a new ScrolledWindow and TextView to create a new tab in the text editor:

def on_imagemenuitemNew_activate(self, *args):
  editor = Gtk.ScrolledWindow()
  editor.add(Gtk.TextView())
  editor.set_shadow_type(Gtk.ShadowType.IN)
  editor.show_all()
  #The instance of Gtk.Notebook is passed to the handler as user data in args[0]
  args[0].append_page(editor, Gtk.Label('untitled'))

This works fine but if I try to use:

editor.connect('focus-in-event', self.on_editor_focus_in_event)

inside the block then it is never registered by my handler:

def on_editor_focus_in_event(self, *args):
  print 'Focused!'

I suspect the issue may be due to each editor instance seemingly being identical but this really has me stumped. Forgive the sloppy coding, I only started learning GTK yesterday, and Pygobject/Gtk3 is not very well documented.


Solution

  • In your code, editor is a Gtk.ScrolledWindow. I don't think ScrolledWindows can have the focus. Try connecting to the focus-in event of your TextView, making sure to enable the correct mask as @TinaBrooks pointed out.