使用 gtk crate 时没有名为 `connect_activate` 的方法

no method named `connect_activate` when using the gtk crate

我正在尝试用 Rust 编写简单的 GTK 应用程序,但遇到无法向菜单项添加信号的问题。有重现问题的简化代码:

空地文件("interface.glade"):

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

Rust 代码 ("main.rs"):

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`

我是不是做错了什么?

Am I doing something wrong?

是的!您没有阅读和解决错误消息:

the method connect_activate exists but the following trait bounds were not satisfied: gtk::widgets::menu_item::MenuItem : gtk::traits::button::ButtonTrait

当然,此错误消息的措辞很晦涩。它是说类型 MenuItem 没有实现特征 ButtonTrait。老实说,这是我第一次看到错误消息的这种特殊措辞,所以我可能有点不对。但是,如果您查看 MenuItem 的文档 (1),您会发现它没有实现 ButtonTrait。这会阻止您调用该方法。

我不知道合适的解决方法是什么。我没有看到任何 examples that use MenuItem。 3 linked 示例项目也不使用它。它完全有可能还没有实现所有适当的特征。也许这对您来说是一个很好的机会,可以为一个崭露头角的项目做出一些承诺! ^_^

我也找不到任何可以让您直接调用 connect 的后备方法或特征。

(1):我希望我可以 link 访问文档,但是我找不到官方托管的版本。