简体   繁体   中英

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 (eg 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.

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?

In your code, editor is a Gtk.ScrolledWindow . I don't think ScrolledWindow s 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM