gpt4 book ai didi

python - 如何在 PyGI GTK 中使用 GIO Actions 创建一个完整的菜单?

转载 作者:太空狗 更新时间:2023-10-29 20:59:01 24 4
gpt4 key购买 nike

我正在尝试转换我的 Gtk 应用程序中的菜单栏,以便它将使用 GActions(来自 Gio)而不是 GtkActions在 Python3 中使用 GObject Instrospection。

我一直在尝试自己解决这个问题,但到目前为止,它看起来非常复杂,而且我的运气并不好。

如果有人可以发一个例子说明如何创建一个简单的菜单 GAction based with

  • 子菜单
  • 带有库存 ID 图标/热键的菜单项
  • 带有非库存图标/热键的菜单项
  • 选中的菜单项
  • 和单选菜单项组
  • 禁用(变灰)的菜单项

这真的对我有很大帮助。

编辑:这是我现在窗口中的菜单栏:

enter image description here

如果有人可以复制使用 GioActions 显示的菜单项,这样我就可以弄清楚它们是如何工作的,那就太好了。

顺便说一句,我使用的所有操作都使用窗口回调而不是应用回调,所以这是窗口菜单栏而不是应用菜单栏。

最佳答案

现在添加了一个菜单栏。

#!/usr/bin/env python3

# Copyright (C) 2013 LiuLang <gsushzhsosgsu@gmail.com>

# Use of this source code is governed by GPLv3 license that can be found
# in http://www.gnu.org/licenses/gpl-3.0.html

from gi.repository import Gio
from gi.repository import Gtk
import sys

menus_str ='''
<?xml version="1.0"?>
<interface>
<menu id="appmenu">
<section>
<item>
<attribute name="label" translatable="yes">Preferences</attribute>
<attribute name="action">app.preferences</attribute>
</item>
</section>
<section>
<item>
<attribute name="label" translatable="yes">About</attribute>
<attribute name="action">app.about</attribute>
</item>
<item>
<attribute name="label" translatable="yes">Quit</attribute>
<attribute name="action">app.quit</attribute>
<attribute name="accel">&lt;Primary&gt;q</attribute>
</item>
</section>
</menu>
<menu id="menubar">
<submenu>
<attribute name="label">_Help</attribute>
<section>
<item>
<attribute name="label">_About</attribute>
<attribute name="action">app.about</attribute>
</item>
</section>
</submenu>
</menu>
</interface>
'''

class App:
def __init__(self):
self.app = Gtk.Application.new('org.liulang.test', 0)
self.app.connect('startup', self.on_app_startup)
self.app.connect('activate', self.on_app_activate)
self.app.connect('shutdown', self.on_app_shutdown)

def run(self, argv):
self.app.run(argv)

def on_app_startup(self, app):
self.window = Gtk.ApplicationWindow.new(app)
self.window.set_default_size(640, 480)
self.window.set_title('Gio Actions Demo')
self.window.set_border_width(5)
# no need to connect delete-event/destroy signal

app.add_window(self.window)

label = Gtk.Label('Hello, Gtk3')
self.window.add(label)
label.props.halign = Gtk.Align.CENTER
label.props.valign = Gtk.Align.CENTER

builder = Gtk.Builder()
# It is better to load ui from a seperate file
builder.add_from_string(menus_str)
builder.connect_signals(self)
appmenu = builder.get_object('appmenu')
app.set_app_menu(appmenu)
menubar = builder.get_object('menubar')
app.set_menubar(menubar)

self.add_simple_action('preferences',
self.on_action_preferences_activated)
self.add_simple_action('about', self.on_action_about_activated)
self.add_simple_action('quit', self.on_action_quit_activated)

def on_app_activate(self, app):
self.window.show_all()

def on_app_shutdown(self, app):
# do some cleaning job here, like dumping configuration.
pass

def on_action_preferences_activated(self, action, user_data):
print('will popup preferences dialog')

def on_action_about_activated(self, action, user_data):
print('will show about dialog')

def on_action_quit_activated(self, action, user_data):
# This will close the default gtk mainloop
self.app.quit()

def add_simple_action(self, name, callback):
action = Gio.SimpleAction.new(name, None)
action.connect('activate', callback)
self.app.add_action(action)


if __name__ == '__main__':
app = App()
app.run(sys.argv)

关于python - 如何在 PyGI GTK 中使用 GIO Actions 创建一个完整的菜单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19481439/

24 4 0