如何在Linux上散焦(模糊)Python-gi GTK + 3窗口 [英] How to unfocus (blur) Python-gi GTK+3 window on Linux

查看:83
本文介绍了如何在Linux上散焦(模糊)Python-gi GTK + 3窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么以及为什么

我希望窗口不聚焦,因此选择了上一个聚焦窗口.

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

为什么?我想与先前选择的窗口(从其他程序)进行交互.我目前的计划是:使窗口不聚焦,使用libxdo模拟按键,然后再次使窗口聚焦.

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

我的窗口可以设置在顶部,以帮助避免轻拂.应该足够好了.对我来说看起来很简单.但是我无法正常工作.

My window can be set on top to help avoid flicking. Should be good enough. Looks simple to me. But I can't get it to work.

到目前为止我尝试过的事情

Gtk.Widget.hide()隐藏窗口,然后再次显示:窗口闪烁太多,并且稍微向顶部移动了一些像素(我想是由于窗口管理器的固执).

Hiding the window with Gtk.Widget.hide() and then showing it again: The window flickers too much and it's slightly moved some pixels to the top (because of window manager stubbornness, I suppose).

示例测试代码

当前代码调用不起作用的Gtk.Window.set_focus(None).我需要用其他可以代替它的线代替它.

Current code calls Gtk.Window.set_focus(None) that does not work. I need to replace that line with something else that makes 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>

推荐答案

一个简单的解决方案是在创建窗口之前然后在每个focus-in-event上记录之前哪个窗口已聚焦,并显式聚焦该窗口而不是试图使活动对象失去焦点:

A simple solution would be to record which window had focus before, both when creating the window and then on each focus-in-event, and explicitly focus that window instead of trying to unfocus 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()

这篇关于如何在Linux上散焦(模糊)Python-gi GTK + 3窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆