1

在 aGtk.Toolbar我有一个Gtk.MenuToolButton带有箭头的我想将 a 关联到Gtk.Menu,最好是在 Glade 中构建。

我的代码是这样的:

self.menuToolButtonCalculate.set_menu(self.get_object("menuCalculate"))

其中self.menuToolButtonCalculate是通过Gtk.Builder.get_object方法获得的,self 是 的子类的一个实例Gtk.Builder

加载窗口时,该程序在终端中显示了一些Gtk/Gdk-CRITICAL警告,当我单击箭头时,程序崩溃了。在显示窗口之前,我还在 Python 代码中填充了Gtk.MenuGlade 中定义的内容,但程序仍然崩溃,所以我现在确定问题不在于菜单为空。

出于测试目的,我用它替换了该行:

self.menuToolButtonCalculate.set_menu(Gtk.Menu())

该程序Gtk/Gdk-CRITICAL在加载窗口时仍会在终端中显示警告,但是当我单击箭头时程序不会崩溃,它会在箭头附近显示一个空的上下文菜单。

这些Gtk/Gdk-CRITICAL警告是:

(python2.exe:14672): Gtk-CRITICAL **: gtk_widget_set_size_request: assertion 'GTK_IS_WIDGET (widget)' failed

(python2.exe:14672): Gtk-CRITICAL **: gtk_container_add: assertion 'GTK_IS_CONTAINER (container)' failed

(python2.exe:14672): Gdk-CRITICAL **: gdk_window_get_height: assertion 'GDK_IS_WINDOW (window)' failed

(python2.exe:14672): Gtk-CRITICAL **: gtk_widget_reparent: assertion 'priv->parent != NULL' failed

(python2.exe:14672): Gdk-CRITICAL **: gdk_window_get_width: assertion 'GDK_IS_WINDOW (window)' failed

(python2.exe:14672): Gdk-CRITICAL **: gdk_device_get_position_double: assertion 'GDK_IS_DEVICE (device)' failed

我使用安装了最新更新的 Windows 10 Pro x64、安装了最新更新的 MSYS2 以及 MSYS2、x86 版本:Python 2.7.13、Glade 3.20 和 GTK+ 3.22。

更新:

简化的测试代码奇怪地完美运行,在终端中没有崩溃或警告:

例子1.glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkMenu" id="menu">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
  </object>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkToolbar">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkMenuToolButton" id="menuToolButton">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">__glade_unnamed_2</property>
            <property name="use_underline">True</property>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="homogeneous">True</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

例子1.py

# coding=utf-8

import gi
gi.require_version("Gtk", "3.0")
from gi.repository import Gtk


b = Gtk.Builder()
b.add_from_file("example1.glade")
w = b.get_object("window1")
mtb = b.get_object("menuToolButton")
m = b.get_object("menu")
mtb.set_menu(m)

w.connect("delete-event", Gtk.main_quit)
w.show_all()
Gtk.main()

原来的程序太大了,不能全部放在这里。请告诉我您希望我发布原始代码中的哪些详细信息,以便您可以帮助我。

更新 2:

在主程序的 Glade .ui 文件中,我定义了 GtkMenu:

<object class="GtkMenu" id="menuCalculate">
  <property name="visible">True</property>
  <property name="can_focus">False</property>
  <property name="tearoff_state">True</property>
  <property name="tearoff_title">Ce dorești să calculez?</property>
</object>

如果我删除最后两个元素(导致程序行为发生变化的元素是tearoff_state

<object class="GtkMenu" id="menuCalculate">
  <property name="visible">True</property>
  <property name="can_focus">False</property>
</object>

当我单击Glade 文件GtkMenuToolButton中声明的 箭头时,程序不会崩溃并且会打开一个空菜单。GtkMenu

所以问题只是由GtkMenu在 Glade 中创建的没有父级的并且tearoff_state属性所要求的引起的。

  1. 如果这是真的,将父级设置GtkMenuGtkMenuToolButton(箭头)的推荐方法是什么,最好在 Glade 中,但至少在代码中?
  2. 据我所见,不可能GtkMenu在 Glade 中填充项目,所以我必须从代​​码中填充它。这是真的?
4

1 回答 1

2

图片值一千字

可以将AGtkMenu分配给GtkMenuToolButtonGlade 中的 a ,菜单内容也可以


填充 GtkMenu

将 GtkMenu 添加到 Glade 项目后,在侧边栏中选择它,您应该会看到一个额外的工具栏图标出现:

编辑按钮

这将打开菜单编辑器:

菜单编辑器

从这里您可以切换到“层次结构”选项卡,您可以在其中使用 + / - 按钮从菜单中添加删除项目

层次结构选项卡


将 GtkMenu 分配给 GtkMentToolButton

现在不幸的是 Glade 不支持这样做,但是底层 GtkBuilder 文件格式按照文档中的说明执行

GtkMenuToolButton 作为 GtkBuildable

GtkBuildable 接口的 GtkMenuToolButton 实现支持通过将“menu”指定为元素的“type”属性来添加菜单。

带有菜单的 UI 定义片段的示例:

<object class="GtkMenuToolButton">
    <child type="menu">
        <object class="GtkMenu"/>
    </child>
</object>

所以点击保存(并退出)并切换到像 Gedit 这样的文本编辑器,我希望你的文件看起来像这样:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkBox">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkToolbar">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuToolButton" id="toolbutton">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">Menu</property>
                <property name="use_underline">True</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="homogeneous">True</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="expand">False</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <placeholder/>
        </child>
      </object>
    </child>
  </object>
  <object class="GtkMenu" id="menu">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <child>
      <object class="GtkMenuItem">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">Yes</property>
        <property name="use_underline">True</property>
      </object>
    </child>
    <child>
      <object class="GtkMenuItem">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">You Can</property>
        <property name="use_underline">True</property>
      </object>
    </child>
    <child>
      <object class="GtkMenuItem">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">Define Menus</property>
        <property name="use_underline">True</property>
      </object>
    </child>
    <child>
      <object class="GtkMenuItem">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="label" translatable="yes">In Glade</property>
        <property name="use_underline">True</property>
      </object>
    </child>
  </object>
</interface>

现在创建一个像这样的<child type="menu">内部:GtkMenuToolButton

<object class="GtkMenuToolButton" id="toolbutton">
    <property name="visible">True</property>
    <property name="can_focus">False</property>
    <property name="label" translatable="yes">Menu</property>
    <property name="use_underline">True</property>
    <child type="menu">
    </child>
</object>

然后剪切并粘贴其中的GtkMenu定义<child type="menu">,不用担心缩进 Glade 会在接下来对其进行排序。

该文件现在应该如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.20.0 -->
<interface>
    <requires lib="gtk+" version="3.20"/>
    <object class="GtkWindow">
        <property name="can_focus">False</property>
        <child>
            <object class="GtkBox">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="orientation">vertical</property>
                <child>
                    <object class="GtkToolbar">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                        <object class="GtkMenuToolButton" id="toolbutton">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="label" translatable="yes">Menu</property>
                            <property name="use_underline">True</property>
                            <child type="menu">
                                <object class="GtkMenu" id="menu">
                                    <property name="visible">True</property>
                                    <property name="can_focus">False</property>
                                    <child>
                                        <object class="GtkMenuItem">
                                            <property name="visible">True</property>
                                            <property name="can_focus">False</property>
                                            <property name="label" translatable="yes">Yes</property>
                                            <property name="use_underline">True</property>
                                        </object>
                                    </child>
                                    <child>
                                        <object class="GtkMenuItem">
                                            <property name="visible">True</property>
                                            <property name="can_focus">False</property>
                                            <property name="label" translatable="yes">You Can</property>
                                            <property name="use_underline">True</property>
                                        </object>
                                    </child>
                                    <child>
                                        <object class="GtkMenuItem">
                                            <property name="visible">True</property>
                                            <property name="can_focus">False</property>
                                            <property name="label" translatable="yes">Define Menus</property>
                                            <property name="use_underline">True</property>
                                        </object>
                                    </child>
                                    <child>
                                        <object class="GtkMenuItem">
                                            <property name="visible">True</property>
                                            <property name="can_focus">False</property>
                                            <property name="label" translatable="yes">In Glade</property>
                                            <property name="use_underline">True</property>
                                        </object>
                                    </child>
                                </object>
                            </child>
                        </object>
                        <packing>
                            <property name="expand">False</property>
                            <property name="homogeneous">True</property>
                        </packing>
                    </child>
                </object>
                <packing>
                    <property name="expand">False</property>
                    <property name="fill">True</property>
                    <property name="position">0</property>
                </packing>
                </child>
                <child>
                    <placeholder/>
                </child>
            </object>
        </child>
    </object>
</interface>

现在保存并关闭文件,然后在 Glade 中再次打开它。

您现在应该看到 glade 将其识别为“菜单子项”,点击保存,Glade 也会为您整理缩进

菜单孩子

在 Windows 10 上使用 Glade 3.20.0

于 2017-08-29T17:43:26.087 回答