Tel*_*Why 1 application-development gtk3 python3
我正在使用 Python3 和 Gtk3 为 Ubuntu 创建一个应用程序。
我正在尝试向程序添加键盘加速器,但不幸的是我一直遇到问题。
这是我的代码自动取款机:
#! /bin/python
from gi.repository import Gtk, Gdk
class DeSedit(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="DeSedit")
self.set_default_size(550, 350)
# FileChooserDialog - Open File
dialog = Gtk.FileChooserDialog("Select file to be opened", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
# keyboard shortcuts
accel = Gtk.AccelGroup()
self.add_accel_group(accel)
self.add_accelerator(dialog, "<Control>o", signal="open")
def add_accelerator(self, widget, accelerator, signal="activate"):
""" adds <Control>o as accelerator for open-file dialog """
if accelerator is not None:
key, mod = Gtk.accelerator_parse(accelerator)
widget.add_accelelator(signal, accel, key, mod, Gtk.AccelFlags.VISIBLE)
print("works")
# grid to organize widgets
self.box = Gtk.Box()
self.add(self.box)
# text view
self.textview = Gtk.TextView()
self.textview.set_wrap_mode(True)
# scroll bar
scrollwindow = Gtk.ScrolledWindow()
scrollwindow.add(self.textview)
self.box.pack_start(scrollwindow, True, True, 0)
window = DeSedit() # create DeSedit object
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()
Run Code Online (Sandbox Code Playgroud)
我收到此错误:
文件“file.py”,第 41 行,在 window = DeSedit() # 创建 DeSedit 对象 文件“file.py”,第 19 行,在 __init__ 中 self.add_accelerator(self.dialog, "o", 信号="打开") 类型错误:add_accelerator() 得到一个意外的关键字参数“信号”
在尝试实现此功能时,您很可能遵循 PyGtk 2 指南:根据 GTK 3 文档,没有名为 的函数gtk_window_add_accelerator
,但有一个名为 的函数gtk_accel_group_connect
,看起来它可以完成工作。在使用 GTK 文档时,通常 80% 的直觉和 20% 的检查文档以查看您是否正确。不要指望有任何花哨的教程或指南(大多数时候你会感到失望)......
总之:纵观gtk_accel_group_connect
文档,我们可以看到这些参数:accel_group
,accel_key
,accel_mods
,accel_flags
和closure
。
那么如何从 Python 中调用这个函数呢?让我们一步一步来:
accel_group
参数是隐式的,因为它是第一个参数,并且是对我们尝试修改的对象的引用。accel_key
) 更棘手:它是一个整数,文档只是告诉我们加速键的键值,这基本上意味着自己弄清楚如何找到加速键值。幸运的是,一些快速搜索揭示了gdk_keyval_from_name
似乎可以做我们想要的功能。(并且调用Gdk.keyval_from_name('O')
实际上返回了正确的结果。)accel_mods
:通过单击它的类型 ( GdkModifierType
),我们可以得到所有可能的修饰符类型的漂亮列表。当您想使用<Control>
修饰符时,我们可以使用Gdk.ModifierType.CONTROL_MASK
并继续。accel_flags
也有一个可点击类型 ( GtkAccelFlags
),打开它的描述会发现不需要这些参数,所以我们可以安全地使用0
这个参数。closure
): 因为closure
它只是回调的一个花哨的词(应该在发生某些事情时调用的东西)我们可以使用 Python GTK 绑定为我们抽象闭包并传递一些可调用函数的事实每当<Control>O
按下时都会调用这里。在上下文中,代码现在看起来像这样(已验证可以正常工作):
#!/usr/bin/python3
from gi.repository import Gtk, Gdk
class DeSedit(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="DeSedit")
self.set_default_size(550, 350)
# FileChooserDialog - Open File
dialog = Gtk.FileChooserDialog("Select file to be opened", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
# keyboard shortcuts
accel = Gtk.AccelGroup()
accel.connect(Gdk.keyval_from_name('O'), Gdk.ModifierType.CONTROL_MASK, 0, self.on_accel_pressed)
self.add_accel_group(accel)
# grid to organize widgets
self.box = Gtk.Box()
self.add(self.box)
# text view
self.textview = Gtk.TextView()
self.textview.set_wrap_mode(True)
# scroll bar
scrollwindow = Gtk.ScrolledWindow()
scrollwindow.add(self.textview)
self.box.pack_start(scrollwindow, True, True, 0)
def on_accel_pressed(self, *args):
dialog = Gtk.FileChooserDialog("Select file to be opened", self,
Gtk.FileChooserAction.OPEN,
(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
Gtk.STOCK_OK, Gtk.ResponseType.OK))
dialog.show()
window = DeSedit() # create DeSedit object
window.connect("delete-event", Gtk.main_quit)
Run Code Online (Sandbox Code Playgroud)
希望这能让您开始使用 GTK+3!
归档时间: |
|
查看次数: |
1664 次 |
最近记录: |