使用gtk板条箱时,没有名为“connect_activate”的方法

使用gtk板条箱时,没有名为“connect_activate”的方法,gtk,rust,Gtk,Rust,我正试图用Rust编写简单的GTK应用程序,但面临着无法向菜单项添加信号的问题。有简化的代码来重现问题: Glade文件(“interface.Glade”): 当我试图编译它时,我得到一个错误: 我做错什么了吗 我做错什么了吗 是的!您没有读取和寻址错误消息: 方法connect\u activate存在,但未满足以下特征边界:gtk::widgets::menu\u项::MenuItem:gtk::traits::button::ButtonTrait 诚然,此错误消息的措辞是迟钝的。这

我正试图用Rust编写简单的GTK应用程序,但面临着无法向菜单项添加信号的问题。有简化的代码来重现问题:

Glade文件(“interface.Glade”):

当我试图编译它时,我得到一个错误:

我做错什么了吗

我做错什么了吗

是的!您没有读取和寻址错误消息:

方法
connect\u activate
存在,但未满足以下特征边界:
gtk::widgets::menu\u项::MenuItem:gtk::traits::button::ButtonTrait

诚然,此错误消息的措辞是迟钝的。这意味着类型
MenuItem
没有实现trait
ButtonTrait
。老实说,这是我第一次看到这个错误消息的特殊措辞,所以我可能有点错误。但是,如果查看
MenuItem
的文档(1),您会发现它没有实现
ButtonTrait
。这将阻止您调用该方法

我不知道什么是合适的解决办法。我没有看到。3个链接的示例项目也不使用它。完全有可能它还没有实现所有合适的特性。也许这将是一个很好的机会,让你得到一些承诺到一个未来的项目^_^

我也找不到任何可以让您直接调用
connect
的回退方法或特性


(1) :我希望可以链接到文档,但我找不到正式托管的版本。

请包括您使用的板条箱版本。如果我尝试使用
gtk 0.0.2
编译您的示例,我会遇到3个不相关的错误。老实说,我已经阅读了错误消息,但认为我忘记了包含一些特性等。当我使用rust时,经常会发生这种情况。但现在我明白了这是一个bug,正如您所说,我可以尝试修复库代码。谢谢您的回答。@Lodin:我在您的船上,与缺少方法相关的错误消息不是非常清楚;很多时候,编译器建议我“使用trait”,因为它已经被使用了,并且问题不满足边界。。。
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.16.1 -->
<interface>
  <requires lib="gtk+" version="3.10"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <object class="GtkBox" id="box1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkMenuBar" id="menubar1">
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <child>
              <object class="GtkMenuItem" id="menuitem1">
                <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="FileMenu">
                        <property name="label">gtk-new</property>
                        <property name="visible">True</property>
                        <property name="can_focus">False</property>
                        <property name="use_underline">True</property>
                        <property name="use_stock">True</property>
                      </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>
      </object>
    </child>
  </object>
</interface>
extern crate gtk;

mod example {
    use gtk;
    use gtk::traits::*;
    use gtk::signal::Inhibit;
    use gtk::widgets::{
        Builder,
        MenuItem
    };
    use gtk::Window;

    pub fn main() {
        gtk::init().unwrap_or_else(|_| panic!("Failed to initialize GTK."));
        let builder = Builder::new_from_file("./interface.glade").unwrap();
        let window: Window = builder.get_object("window1").unwrap();

        window.connect_delete_event(|_, _| {
            gtk::main_quit();
            Inhibit(true)
        });

        let file_menu: MenuItem = builder.get_object("FileMenu").unwrap();
        file_menu.connect_activate(|_| {
            println!("Activated");
        });

        window.show_all();
        gtk::main();
    }
}

fn main() {
    example::main()
}
src/main.rs:24:19: 26:11 error: no method named `connect_activate` found for type `gtk::widgets::menu_item::MenuItem` in the current scope
src/main.rs:24         file_menu.connect_activate(|_| {
src/main.rs:25             println!("Activated");
src/main.rs:26         });
src/main.rs:24:19: 26:11 note: the method `connect_activate` exists but the following trait bounds were not satisfied: `gtk::widgets::menu_item::MenuItem : gtk::traits::button::ButtonTrait`