Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在pygigtk中使用GIO操作创建完整的菜单?_Python_Python 3.x_Gtk_Gtk3_Pygobject - Fatal编程技术网

Python 如何在pygigtk中使用GIO操作创建完整的菜单?

Python 如何在pygigtk中使用GIO操作创建完整的菜单?,python,python-3.x,gtk,gtk3,pygobject,Python,Python 3.x,Gtk,Gtk3,Pygobject,我正在尝试转换我的Gtk应用程序中的菜单栏,以便它使用GActions(来自Gio),而不是使用GObject Instropection的Python3中的GtkActions 我一直在试图自己解决这个问题,但到目前为止,它似乎非常复杂,我没有太多的运气 如果有人可以发布一个示例,说明如何基于 子菜单 带有股票ID图标/热键的菜单项 带有非库存图标/热键的菜单项 选中的菜单项 和收音机菜单项组 已禁用(灰显)的菜单项 这对我真的有很大帮助 编辑:这是我现在窗口中的菜单栏: 如果有人能用G

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

我一直在试图自己解决这个问题,但到目前为止,它似乎非常复杂,我没有太多的运气

如果有人可以发布一个示例,说明如何基于

  • 子菜单
  • 带有股票ID图标/热键的菜单项
  • 带有非库存图标/热键的菜单项
  • 选中的菜单项
  • 和收音机菜单项组
  • 已禁用(灰显)的菜单项
这对我真的有很大帮助

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

如果有人能用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)
#/usr/bin/env蟒蛇3
#版权所有(C)2013流浪
#此源代码的使用受可以找到的GPLv3许可证的约束
#在http://www.gnu.org/licenses/gpl-3.0.html
从gi.repository导入Gio
从gi.repository导入Gtk
导入系统
菜单\u str='''
偏好
app.preferences
关于
app.about
退出
app.quit
Primaryq
_帮助
_关于
app.about
'''
类应用程序:
定义初始化(自):
self.app=Gtk.Application.new('org.liulang.test',0)
self.app.connect('startup',self.on\u app\u startup)
self.app.connect('activate',self.on\u app\u activate)
self.app.connect('shutdown',self.on\u app\u shutdown)
def运行(自身,argv):
self.app.run(argv)
应用程序启动时的def(自身、应用程序):
self.window=Gtk.ApplicationWindow.new(应用程序)
self.window.set\u默认大小(640480)
self.window.set_title('Gio Actions Demo')
self.window.set_边框_宽度(5)
#无需连接删除事件/销毁信号
应用程序添加窗口(自窗口)
label=Gtk.label('Hello,Gtk3')
self.window.add(标签)
label.props.halign=Gtk.Align.CENTER
label.props.valign=Gtk.Align.CENTER
builder=Gtk.builder()
#最好从单独的文件加载ui
生成器。从字符串添加字符串(菜单)
建造商。连接信号(自)
appmenu=builder.get\u对象(“appmenu”)
应用程序设置应用程序菜单(应用程序菜单)
menubar=builder.get\u对象('menubar')
应用程序设置菜单栏(菜单栏)
添加简单动作(“首选项”,
自启动(启动操作首选项)
self.add\u简单动作('about',self.on\u动作\u about\u激活)
self.add\u简单操作('quit',self.on\u操作\u quit\u激活)
应用程序上的def激活(自我,应用程序):
self.window.show_all()
def on_应用程序_关机(自身,应用程序):
#在这里做一些清洁工作,比如卸载配置。
通过
def on_action_preferences_已激活(自身、操作、用户数据):
打印('将弹出首选项对话框')
def on_action_about_activated(自我、操作、用户数据):
打印('将显示关于对话框')
def on_action_quit_已激活(自身、操作、用户数据):
#这将关闭默认的gtk主回路
self.app.quit()
def添加简单操作(自身、名称、回调):
action=Gio.SimpleAction.new(名称,无)
action.connect('activate',callback)
self.app.add_动作(动作)
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
app=app()
应用程序运行(sys.argv)

你真的应该编辑以前的答案,而不是发布新答案。此外,这些不包括检查菜单项或收音机菜单项:苏,来吧。我应该为您写出整个应用程序吗?有没有一种方法可以让
Menubar
Toolbar
不使用从
Gtk.Application
派生的类?