如何将钥匙按下与GTK4中的特定动作绑定?
这是在这里:
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk
def on_activate(app):
win = Gtk.ApplicationWindow(application=app)
btn = Gtk.Button(label="Hello, World!")
btn.connect('clicked', lambda x: win.close())
win.set_child(btn)
win.present()
app = Gtk.Application(application_id='org.gtk.Example')
app.connect('activate', on_activate)
app.run(None)
我的目标是使应用程序关闭,我的目标是使应用程序关闭在按键q
中,该窗口中的任何位置。这也是我的问题:如何使用GTK4实现这一目标?
我的尝试
1。
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk
def on_activate(app):
win = Gtk.ApplicationWindow(application=app)
win.connect('key-pressed', lambda x,y: win.close())
btn = Gtk.Button(label="Hello, World!")
btn.connect('clicked', lambda x: win.close())
win.set_child(btn)
win.present()
app = Gtk.Application(application_id='org.gtk.Example')
app.connect('activate', on_activate)
app.run(None)
使用python file.py
运行该代码给出此错误:
Traceback (most recent call last):
File "/home/caveman/lol/file.py", line 7, in on_activate
app.connect('key-pressed', lambda x,y: win.close())
TypeError: <Gtk.Application object at 0x7fcd657fe600 (GtkApplication at 0x55bdc34c5210)>: unknown signal name: key-pressed
2。
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk
class PressableWindow(Gtk.ApplicationWindow, Gtk.EventControllerKey):
def __init__(self, app):
super().__init__(application=app)
def on_activate(app):
win = PressableWindow(app)
win.connect('key-pressed', lambda x,y: win.close())
btn = Gtk.Button(label="Hello, World!")
btn.connect('clicked', lambda x: win.close())
win.set_child(btn)
win.present()
app = Gtk.Application(application_id='org.gtk.Example')
app.connect('activate', on_activate)
app.run(None)
给出错误:
Traceback (most recent call last):
File "/home/caveman/lol/file.py", line 11, in on_activate
win.connect('key-pressed', lambda x,y: win.close())
TypeError: <__main__.PressableWindow object at 0x7f8838b5f8c0 (__main__+PressableWindow at 0x555bddea82f0)>: unknown signal name: key-pressed
This is an example from here:
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk
def on_activate(app):
win = Gtk.ApplicationWindow(application=app)
btn = Gtk.Button(label="Hello, World!")
btn.connect('clicked', lambda x: win.close())
win.set_child(btn)
win.present()
app = Gtk.Application(application_id='org.gtk.Example')
app.connect('activate', on_activate)
app.run(None)
My goal is to make the application close itself upon pressing key q
anywhere in that window. This is also my question: how to achieve this with GTK4?
My attempts
1.
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk
def on_activate(app):
win = Gtk.ApplicationWindow(application=app)
win.connect('key-pressed', lambda x,y: win.close())
btn = Gtk.Button(label="Hello, World!")
btn.connect('clicked', lambda x: win.close())
win.set_child(btn)
win.present()
app = Gtk.Application(application_id='org.gtk.Example')
app.connect('activate', on_activate)
app.run(None)
Running that code with python file.py
gives this error:
Traceback (most recent call last):
File "/home/caveman/lol/file.py", line 7, in on_activate
app.connect('key-pressed', lambda x,y: win.close())
TypeError: <Gtk.Application object at 0x7fcd657fe600 (GtkApplication at 0x55bdc34c5210)>: unknown signal name: key-pressed
2.
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import Gtk
class PressableWindow(Gtk.ApplicationWindow, Gtk.EventControllerKey):
def __init__(self, app):
super().__init__(application=app)
def on_activate(app):
win = PressableWindow(app)
win.connect('key-pressed', lambda x,y: win.close())
btn = Gtk.Button(label="Hello, World!")
btn.connect('clicked', lambda x: win.close())
win.set_child(btn)
win.present()
app = Gtk.Application(application_id='org.gtk.Example')
app.connect('activate', on_activate)
app.run(None)
Gives error:
Traceback (most recent call last):
File "/home/caveman/lol/file.py", line 11, in on_activate
win.connect('key-pressed', lambda x,y: win.close())
TypeError: <__main__.PressableWindow object at 0x7f8838b5f8c0 (__main__+PressableWindow at 0x555bddea82f0)>: unknown signal name: key-pressed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这解决了。显然,这是GTK4方法。
注1。显然有GTKACTION,我想也可以用来创建命令绑定。我仍在寻找这一点,因为我的情况并不需要将这些动作绑定到任何GUI元素。但是就这个问题而言,这可以回答。
注2。整个代码非常脏,是从GTK文档的早期示例中获取的。更好的代码可能会使用,所以我不会通过
手动传递
。致谢。 > #python in
irc.gnome.org
。This solves it. Apparently this is the GTK4 approach.
Note 1. Apparently there is GtkAction which I guess can be used to create command bindings too. I'm still looking for that, since my scenario does not really require binding such actions to any GUI element. But as far as this question goes, this answers it.
Note 2. The whole code is very dirty and is taken from early examples from GTK's documentation. A better code would probably make use of classes, so I don't pass
win
manually.Acknowledgements. Thanks to people in
#gtk
and#python
inirc.gnome.org
.