Python gtk3 window 监听输入事件
Python gtk3 window listen to input events
有什么方法可以让 python gtk3 Window 捕获来自键盘 and/or 鼠标的每个输入吗?
基本上我正在创建一个应用程序,它会在空闲时启动类似屏幕保护程序的东西,为此我需要知道键盘或鼠标事件在我的window(应用程序)中发生的时间。
我尝试将 key-press-event
连接到我的 window,但是当我点击它的子元素时,就像按钮一样,window 没有得到事件。
感谢您提供任何解决方案。
更新
正如公认的答案所建议的那样,我将 event-after
信号连接到主 window,并确定是否需要接收信号。
key-press-event
仅适用于键盘。一个按钮创建一个 button-press-event
。参见docs。
永远记住 *-press-event
在操作完成之前得到 运行。如果您不小心单击了按钮,我建议您以这种方式使用 *-release-event
,您可以在松开按钮之前滑出按钮。这对你的情况并不重要,但仍然......
也许首先回忆起有信号和事件。也许有点过于简单化,事件是 'raw':例如,它们来自键盘或鼠标。最上面的小部件(考虑到 window 在底部)接收这些事件并决定它是否对事件感兴趣。
如果小部件对事件感兴趣,例如对按钮按下事件和按钮释放事件感兴趣的按钮,则小部件然后创建信号,例如,在这种情况下,'clicked', 'double-clicked' 信号。在事件处理程序中,return 值(True 或 False)确定事件是否将 'down' 传播到下一个小部件('under' 顶部)。
因此,我怀疑您无法在底层 window 达到目标之前直接监控所有事件。
也许您的解决方案是连接到 window 的 'event-after'。这是独立于其他处理程序的 return 值调用的。下面是一个小程序,您可以在其中测试以下内容:
出现一个带有标签、按钮和旋转按钮的 GtkGrid。右下单元格为空:
点击标签,事件会向下滴入 window,因为标签不捕获任何事件
点击按钮,事件(如'pressed')被handler捕获,产生'clicked'等信号
我连接到 'event-after',然后报告在任何地方按下按钮。
.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# test_events.py
#
# Copyright 2017 John Coppens <john*at*jcoppens*dot*com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from gi.repository import Gtk
class EventsTest(Gtk.Grid):
def __init__(self):
super(EventsTest, self).__init__()
btn = Gtk.Button("Top right")
btn.connect("clicked", lambda x: print("Button clicked"))
self.attach(Gtk.Label("Top left"), 0, 0, 1, 1)
self.attach(btn, 1, 0, 1, 1)
self.attach(Gtk.SpinButton(), 0, 1, 1, 1)
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.connect("destroy", lambda x: Gtk.main_quit())
self.connect("button-press-event", self.on_button_pressed)
self.connect("event-after", self.on_event_after)
evtest = EventsTest()
self.add(evtest)
self.show_all()
def on_button_pressed(self, btn, event):
print("Main window button pressed")
return True
def on_event_after(self, wdg, event):
print("Event after")
def run(self):
Gtk.main()
def main(args):
mainwdw = MainWindow()
mainwdw.run()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
有什么方法可以让 python gtk3 Window 捕获来自键盘 and/or 鼠标的每个输入吗?
基本上我正在创建一个应用程序,它会在空闲时启动类似屏幕保护程序的东西,为此我需要知道键盘或鼠标事件在我的window(应用程序)中发生的时间。
我尝试将 key-press-event
连接到我的 window,但是当我点击它的子元素时,就像按钮一样,window 没有得到事件。
感谢您提供任何解决方案。
更新
正如公认的答案所建议的那样,我将 event-after
信号连接到主 window,并确定是否需要接收信号。
key-press-event
仅适用于键盘。一个按钮创建一个 button-press-event
。参见docs。
永远记住 *-press-event
在操作完成之前得到 运行。如果您不小心单击了按钮,我建议您以这种方式使用 *-release-event
,您可以在松开按钮之前滑出按钮。这对你的情况并不重要,但仍然......
也许首先回忆起有信号和事件。也许有点过于简单化,事件是 'raw':例如,它们来自键盘或鼠标。最上面的小部件(考虑到 window 在底部)接收这些事件并决定它是否对事件感兴趣。
如果小部件对事件感兴趣,例如对按钮按下事件和按钮释放事件感兴趣的按钮,则小部件然后创建信号,例如,在这种情况下,'clicked', 'double-clicked' 信号。在事件处理程序中,return 值(True 或 False)确定事件是否将 'down' 传播到下一个小部件('under' 顶部)。
因此,我怀疑您无法在底层 window 达到目标之前直接监控所有事件。
也许您的解决方案是连接到 window 的 'event-after'。这是独立于其他处理程序的 return 值调用的。下面是一个小程序,您可以在其中测试以下内容:
出现一个带有标签、按钮和旋转按钮的 GtkGrid。右下单元格为空:
点击标签,事件会向下滴入 window,因为标签不捕获任何事件
点击按钮,事件(如'pressed')被handler捕获,产生'clicked'等信号
我连接到 'event-after',然后报告在任何地方按下按钮。
.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# test_events.py
#
# Copyright 2017 John Coppens <john*at*jcoppens*dot*com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
# MA 02110-1301, USA.
#
#
from gi.repository import Gtk
class EventsTest(Gtk.Grid):
def __init__(self):
super(EventsTest, self).__init__()
btn = Gtk.Button("Top right")
btn.connect("clicked", lambda x: print("Button clicked"))
self.attach(Gtk.Label("Top left"), 0, 0, 1, 1)
self.attach(btn, 1, 0, 1, 1)
self.attach(Gtk.SpinButton(), 0, 1, 1, 1)
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.connect("destroy", lambda x: Gtk.main_quit())
self.connect("button-press-event", self.on_button_pressed)
self.connect("event-after", self.on_event_after)
evtest = EventsTest()
self.add(evtest)
self.show_all()
def on_button_pressed(self, btn, event):
print("Main window button pressed")
return True
def on_event_after(self, wdg, event):
print("Event after")
def run(self):
Gtk.main()
def main(args):
mainwdw = MainWindow()
mainwdw.run()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))