也许首先记得有信号和事件。也许有些过于简单,事件是“原始的”:例如,它们来自键盘或鼠标。最顶部的小部件(考虑到窗口位于底部)接收这些事件并决定它是否对事件感兴趣。
如果小部件对事件感兴趣,比如按钮对按钮按下事件和按钮释放事件感兴趣,那么小部件就会创建信号,例如,在这种情况下,“点击”、“双击'信号。在事件处理程序内部,返回值(True 或 False)决定了事件是否会“向下”传播到下一个小部件(在顶部小部件的“下方”)。
因此,我怀疑您无法在底层窗口上直接监控所有事件,然后才能达到目标。
也许您的解决方案是连接到窗口的“事件之后”。这被独立于其他处理程序的返回值调用。下面是一个小程序,您可以在其中测试以下内容:
GtkGrid 出现时带有一个标签、一个按钮和一个 SpinButton。右下角单元格为空:
.
#!/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))