0

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.

Nick Hu
  • 43
  • 3
  • Maybe this can help? https://mail.gnome.org/archives/gtk-app-devel-list/2012-February/msg00052.html – Kristina Aug 22 '12 at 21:09

2 Answers2

1

I can only make a guess, but the documentation says that "to receive this signal, the GdkWindow associated to the widget needs to enable the GDK_FOCUS_CHANGE_MASK mask".

Are you sure you have it enabled?

Kristina
  • 15,859
  • 29
  • 111
  • 181
  • In PyGObject, that particular enum is `Gdk.EventMask.FOCUS_CHANGE_MASK` Also, it does not work even with that mask set. – Nick Hu Aug 22 '12 at 21:22
1

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.

ptomato
  • 56,175
  • 13
  • 112
  • 165
  • 1
    That did the trick! Thank you! With this it also seems works without the mask. I just needed to do this: `editor.get_child().connect('focus-in-event', self.on_editor_focus_in_event)` – Nick Hu Aug 22 '12 at 22:32