如何使用一些GtkMenuItem填充GtkMenu并在Glade中设置其父级?

时间:2017-08-27 00:50:50

标签: python-2.7 gtk3 glade msys2 gtkbuilder

Gtk.Toolbar Gtk.MenuToolButton我有一个Gtk.Menu箭头,我想将self.menuToolButtonCalculate.set_menu(self.get_object("menuCalculate")) self.menuToolButtonCalculate相关联,最好是用Glade制作。

我的代码是这样的:

Gtk.Builder.get_object

其中Gtk.Builder是通过Gtk/Gdk-CRITICAL方法获得的,而self是Gtk.Menu的子类的实例。

当窗口加载时,程序在终端显示一些self.menuToolButtonCalculate.set_menu(Gtk.Menu()) 警告,当我点击箭头时程序崩溃了。在显示窗口之前,我还在Python代码中填充了Glade中定义的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 警告,但是当我单击箭头时程序不会崩溃,它会在箭头附近显示一个空的上下文菜单。

这些<?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> 警告是:

# 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()

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

更新

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

example1.glade

<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>

example1.py

tearoff_state

原始程序太大了,无法将其全部放在这里。请告诉我您希望我发布原始代码的详细信息,以便您可以帮助我。

更新2:

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

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

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

GtkMenu

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

所以这个问题只是由Glade中创建的GtkMenu引起的,该GtkMenuToolButton没有父级,而GtkMenu属性需要这个。{/ p>

  1. 如果这是真的,建议将:%!cut -d":" -f1 的父级设置为:help :range!(&#39; s箭头)的方法是什么,最好是在Glade中,但至少在代码中?< / LI>
  2. 从我看到的情况来看,不可能在Glade中填充var colors = ['#ec008c', '#00bcc3', '#5fb26a', '#fc7331']; $(document).ready(function( $ ){ setInterval(function(){ var color = colors.shift(); colors.push(color); $('.colours a.button').each(function() {$(this).animate({backgroundColor: color}, 1500)}); $('.colours h1').each(function() {$(this).animate({color: color}, 1500)}); },4000); // Text Colour Cycling }); 项,所以我必须从代​​码中填充它。这是真的吗?

1 个答案:

答案 0 :(得分:1)

Picture is worth a 1000 words

可以将GtkMenu分配给Glade中的GtkMenuToolButton,菜单内容也可以

填充GtkMenu

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

Edit button

这将打开菜单编辑器:

Menu Editor

从此处您可以切换到“层次结构”&#39;选项卡,您可以使用+ / - 按钮从菜单中添加删除项目

Hierarchy Tab

将GtkMenu分配给Gtk​​MentToolButton

现在,Glade并不支持这样做但是基础GtkBuilder文件格式as explained in the docs

  

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>

然后切割&amp;将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也会为您排序缩进

menu child

在Windows 10上使用Glade 3.20.0