似乎没有一种简单的方法来禁用默认的“单击”处理程序。显然,该点击处理程序正在窃取用户的'已按'事件。如果你足够快,你可以看到发生这种情况(至少这是我的解释)。点击一个按钮时,它会等待几百毫秒来检查是否发生了另一次点击(双击事件)。在此期间,新闻事件做通过。测试运行下面的程序,点击按钮反复(和快速):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# test_toolbar_press.py
#
# Copyright 2016 John Coppens <[email protected]>
#
# 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, Gdk
class MyToolbar(Gtk.Toolbar):
def __init__(self):
super(MyToolbar, self).__init__()
btn = Gtk.ToolButton(None, None)
btn.connect("button-press-event", self.btn_pressed)
btn.connect_after("clicked", self.btn_clicked)
self.insert(btn, 0)
def btn_pressed(self, wdg, event):
print("Button pressed")
def btn_clicked(self, wdg):
print("Button clicked")
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.connect("destroy", lambda x: Gtk.main_quit())
self.set_size_request(200, 50)
tbar = MyToolbar()
self.add(tbar)
self.show_all()
def run(self):
Gtk.main()
def main(args):
mainwdw = MainWindow()
mainwdw.run()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
点击缓慢显示了正常的“点击次数”,更快也显示了“按下”:
我建议以下解决方案 - 尽管它不是一个工具栏,你可以做这样的变化,以适应您的设计:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# test_toolbar_press.py
#
# Copyright 2016 John Coppens <[email protected]>
#
# 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, Gdk
class MyNavbar(Gtk.Grid):
def __init__(self):
super(MyNavbar, self).__init__()
for dir, x, y in (("N", 1, 0), ("E", 2, 1), ("S", 1, 2), ("W", 0, 1)):
btn = Gtk.Button(dir, None)
btn.connect("button-press-event", self.btn_pressed, dir)
btn.connect("button-release-event", self.btn_released, dir)
self.attach(btn, x, y, 1, 1)
def btn_pressed(self, wdg, event, data):
print("Button pressed %s" % data)
def btn_released(self, wdg, event, data):
print("Button released %s" % data)
class MainWindow(Gtk.Window):
def __init__(self):
super(MainWindow, self).__init__()
self.connect("destroy", lambda x: Gtk.main_quit())
self.set_size_request(200, 50)
tbar = MyNavbar()
self.add(tbar)
self.show_all()
def run(self):
Gtk.main()
def main(args):
mainwdw = MainWindow()
mainwdw.run()
return 0
if __name__ == '__main__':
import sys
sys.exit(main(sys.argv))
这是结果:
,这是程序的输出:
Button pressed N
Button released N
Button pressed E
Button released E
Button pressed S
Button released S
Button pressed W
Button released W
我的道歉在Python中的例子。但是,将基本Gtk函数转换为C应该不会太困难。
问题是它没有达到回调。我提供了断点,但调试过程并不止于此,尽管我将所有回调内容替换为printf + return。我做了一个[MWE](https://gist.github.com/anderflash/b9cffe559f3981e3fc63e9e0d4eb63ac) – Anderson
别担心。也许这是一个Gtk +设计决定。所以我可以重新考虑重新设计不使用GtkToolbar的界面。目前,我添加了键盘快捷键来完成翻译。 – Anderson
对[GtkToolButton源代码](https://github.com/GNOME/gtk/blob/master/gtk/gtktoolbutton.c)的一点检查表明,GtkToolButton只是一个拥有GtkButton子项的容器,我们不会无法访问它。只需将按钮的'click'事件传播到工具按钮即可。我认为这种封装失去了几个应用的灵活性。 GtkToolButton可以传播其他GtkButton事件,或者添加一个访问器到按钮。 – Anderson