How to maximize (blur) a Python-gi GTK + 3 window on Linux

What I want to do and why

I want my window to not focus, so the previous focused window is selected.

Why? I want to interact with a previously selected window (from other programs). My current plan is to expand my window, use libxdo to simulate keystrokes, and then focus my window again.

My window can be mounted on top to avoid clicking. It should be good enough. It looks simple to me. But I can’t make it work.

What have i tried so far

Hiding the window with Gtk.Widget.hide() and then re-displaying: the window flickers too much and it is slightly pushed a few pixels up (due to the stubbornness of the window manager, I suppose).

Test code example

Current code calls Gtk.Window.set_focus(None) , that do not work. I need to replace this line with something else that does what I want it to do.

losefocus.py :

 import signal from gi import require_version require_version('Gtk', '3.0') from gi.repository import GLib, Gtk, GObject class LoseFocusHandler: def onClick(self, window): print "Losing focus yet?" window1 = builder.get_object("window1") window1.set_focus(None) if __name__ == "__main__": GObject.threads_init() builder = Gtk.Builder() builder.add_from_file("losefocus.glade") builder.connect_signals(LoseFocusHandler()) window1 = builder.get_object("window1") window1.show_all() signal.signal(signal.SIGINT, signal.SIG_DFL) Gtk.main() 

losefocus.glade :

 <?xml version="1.0" encoding="UTF-8"?> <!-- Generated with glade 3.16.1 --> <interface> <requires lib="gtk+" version="3.10"/> <object class="GtkWindow" id="window1"> <property name="can_focus">False</property> <property name="window_position">center-always</property> <property name="gravity">center</property> <child> <object class="GtkButton" id="button1"> <property name="label" translatable="yes">Lose Focus!</property> <property name="visible">True</property> <property name="can_focus">True</property> <property name="receives_default">True</property> <property name="relief">half</property> <signal name="clicked" handler="onClick" swapped="no"/> </object> </child> </object> </interface> 
+5
source share
1 answer

A simple solution would be to record in which window it was focused before, both when creating the window and during each focus-in-event , and to explicitly focus this window instead of trying to focus the active one:

 import signal from gi import require_version require_version('Gtk', '3.0') from gi.repository import GLib, Gdk, Gtk, GObject class LoseFocusHandler: def onClick(self, window): print "Losing focus yet?" old_window[0].focus(0) def focus_handler(gdk_window, event): # At this point, our window does not have focus yet, but is # about to. This hence works: old_window[0] = gdk_window.get_screen().get_active_window() if __name__ == "__main__": GObject.threads_init() old_window = [ Gdk.Screen.get_default().get_active_window() ] builder = Gtk.Builder() builder.add_from_file("losefocus.glade") builder.connect_signals(LoseFocusHandler()) window1 = builder.get_object("window1") window1.connect("focus-in-event", focus_handler) window1.show_all() signal.signal(signal.SIGINT, signal.SIG_DFL) Gtk.main() 
+1
source

Source: https://habr.com/ru/post/1245138/


All Articles