繁体   English   中英

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

[英]no method named `connect_activate` when using the gtk crate

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

Glade 文件(“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`

难道我做错了什么?

难道我做错了什么?

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

方法connect_activate存在但不满足以下特征边界: gtk::widgets::menu_item::MenuItem : gtk::traits::button::ButtonTrait

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM