我不知道如何将函数添加到glade / python中的菜单项(快速生成)

时间:2015-08-15 17:14:18

标签: python ubuntu-14.04 glade

我试图在单击时向菜单项添加一个函数我希望该函数执行

该应用程序名为Simpleadministration

我正在使用的代码位于Simpleadministrationwindow.py:

requiredTextField.pressForDuration:0.5()
app.menuItems[@"Select All"].tap()
app.menuItems[@"Cut"].tap()

终端警告:

## functions
    class Handler:
        def on_account_add_menu_item_activate():
            print ("something")

    builder = Gtk.Builder()
    builder.add_from_file("data/ui/SimpleadministrationWindow.ui")
    builder.connect_signals(Handler())  

如果有人帮助我,我不确切地知道出了什么问题?

额外的inf:

(SimpleadministrationWindow.ui中所选项目的菜单结构) http://i57.tinypic.com/2s6pstx.png(无法上传信誉低于10)

尽量提供尽可能多的信息

(SimpleadministrationWindow.ui)

WARNING:simpleadministration_lib: connect_signals() 'expected handler 'on_account_add_menu_item_activate' in /home/ami/simpleadministration/simpleadministration/SimpleadministrationWindow.py'

这个现在有效,但我不认为你应该这样做,你可能不应该做的其他事情是让建设者全球化,但我不知道我是怎么做的可以用另一种方式访问​​我的窗口(我应该发布另一个问题),但我认为我的整个基础设施需要改变

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.18.3 -->
<interface>
  <requires lib="gtk+" version="3.0"/>
  <requires lib="simpleadministration_window" version="1.0"/>
  <!-- interface-local-resource-path ../media -->
  <object class="SimpleadministrationWindow" id="simpleadministration_window">
    <property name="can_focus">False</property>
    <property name="title" translatable="yes">Simpleadministration</property>
    <property name="icon">../media/simpleadministration.svg</property>
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="spacing">5</property>
        <child>
          <object class="GtkMenuBar" id="menubar1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem" id="mnu_file">
                <property name="use_action_appearance">False</property>
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <property name="label" translatable="yes">_File</property>
                <property name="use_underline">True</property>
                <child type="submenu">
                  <object class="GtkMenu" id="menu1">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <child>
                      <object class="GtkImageMenuItem" id="mnu_new">
                        <property name="label">gtk-new</property>
                        <property name="use_action_appearance">False</property>
                        <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                    <accelerator key="n" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                  </object>
                </child>
                <child>
                  <object class="GtkImageMenuItem" id="mnu_open">
                    <property name="label">gtk-open</property>
                    <property name="use_action_appearance">False</property>
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                    <accelerator key="o" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                  </object>
                </child>
                <child>
                  <object class="GtkSeparatorMenuItem" id="separatormenuitem3">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                  </object>
                </child>
                <child>
                  <object class="GtkImageMenuItem" id="mnu_save">
                    <property name="label">gtk-save</property>
                    <property name="use_action_appearance">False</property>
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="use_underline">True</property>
                    <property name="use_stock">True</property>
                    <accelerator key="s" signal="activate" modifiers="GDK_CONTROL_MASK"/>
                  </object>
                </child>
              </object>
            </child>
          </object>
        </child>
        <child>
          <object class="GtkMenuItem" id="mnu_edit">
            <property name="use_action_appearance">False</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">_Edit</property>
            <property name="use_underline">True</property>
            <child type="submenu">
              <object class="GtkMenu" id="menu2">
                <property name="visible">True</property>
                <property name="can_focus">False</property>
                <child>
                  <object class="GtkMenuItem" id="account_menu_item">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="label" translatable="yes">account</property>
                    <property name="use_underline">True</property>
                    <child type="submenu">
                      <object class="GtkMenu" id="menu3">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <child>
                          <object class="GtkMenuItem" id="account_add_menu_item">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="label" translatable="yes">add</property>
                            <property name="use_underline">True</property>
                            <signal name="activate" handler="on_account_add_menu_item_activate" swapped="no"/>
                          </object>
                        </child>
                        <child>
                          <object class="GtkMenuItem" id="account_change_status_menu_item">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="label" translatable="yes">change status</property>
                            <property name="use_underline">True</property>
                          </object>
                        </child>
                      </object>
                    </child>
                  </object>
                </child>
                <child>
                  <object class="GtkMenuItem" id="budget_menu_item">
                    <property name="visible">True</property>
                    <property name="can_focus">False</property>
                    <property name="label" translatable="yes">budget</property>
                    <property name="use_underline">True</property>
                    <child type="submenu">
                      <object class="GtkMenu" id="menu4">
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <child>
                          <object class="GtkMenuItem" id="budget_add_menu_item">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="label" translatable="yes">add</property>
                            <property name="use_underline">True</property>
                          </object>
                        </child>
                        <child>
                          <object class="GtkMenuItem" id="budget_change_menu_item">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="label" translatable="yes">change</property>
                            <property name="use_underline">True</property>
                          </object>
                        </child>
                        <child>
                          <object class="GtkMenuItem" id="budget_delete_menu_item">
                            <property name="visible">True</property>
                            <property name="can_focus">False</property>
                            <property name="label" translatable="yes">delete</property>
                            <property name="use_underline">True</property>
                          </object>
                        </child>
                      </object>
                    </child>
                  </object>
                </child>
              </object>
            </child>
          </object>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="position">0</property>
      </packing>
    </child>
    <child>
      <placeholder/>
    </child>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkStatusbar" id="statusbar1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="spacing">2</property>
        <child>
          <object class="GtkLabel" id="label2">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="xalign">0</property>
            <property name="xpad">5</property>
            <property name="ypad">5</property>
            <property name="label" translatable="yes">Status Area</property>
          </object>
          <packing>
            <property name="expand">True</property>
            <property name="fill">True</property>
            <property name="position">0</property>
          </packing>
        </child>
      </object>
      <packing>
        <property name="expand">False</property>
        <property name="fill">True</property>
        <property name="pack_type">end</property>
        <property name="position">3</property>
      </packing>
    </child>
  </object>
</child>
  </object>
</interface>

1 个答案:

答案 0 :(得分:0)

我建议再看一些关于如何构造类的例子。这个问题的答案包含一个工作示例,您应该能够快速地将其调整到您的程序中:

您看到的警告意味着builder.connect_signals(Handler())方法找不到您在Glade中定义的处理程序。所以也许这个类没有正确初始化(检查执行顺序?)。

如果您的项目是开源的,您可以将其上传到人们可以运行整个程序并建议改进的地方。您还可以查看this程序,了解有关如何构建更大项目的一些提示。