Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我们如何使用glade和rust中的gtk rs构建GUI?_Rust_Gtk_Glade_Gtk Rs - Fatal编程技术网

我们如何使用glade和rust中的gtk rs构建GUI?

我们如何使用glade和rust中的gtk rs构建GUI?,rust,gtk,glade,gtk-rs,Rust,Gtk,Glade,Gtk Rs,我使用glade创建了一个简单的GUI,其中包含一个窗口、输入框、标签和一个按钮,并在我的rust项目的src目录中保存为example.glade <?xml version="1.0" encoding="UTF-8"?> <!-- Generated with glade 3.22.1 --> <interface> <requires lib="gtk+" version="

我使用glade创建了一个简单的GUI,其中包含一个窗口、输入框、标签和一个按钮,并在我的rust项目的src目录中保存为example.glade

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow">
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkFixed" id="windows1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="x">182</property>
            <property name="y">146</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="box1">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
          </object>
          <packing>
            <property name="x">68</property>
            <property name="y">45</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">label</property>
          </object>
          <packing>
            <property name="x">321</property>
            <property name="y">44</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

由于缺乏良好的文档、教程或帮助,在rust中编程非常困难。我必须梳理许多网站,复制过去的代码,编辑许多示例代码,才能让这件事顺利进行。首先,我们必须在cargo.toml文件中指定一个版本,该版本必须是我们安装的gtk3、gio版本。现在我的依赖项看起来像这样

[dependencies]
glib = "0.10.3"

[dependencies.gtk]
version = "0.9.2"
features = ["v3_XX"] // XX our gtk3 sub version

[dependencies.gio]
version = "0.9.1"
features = ["v2_XX"] // XX our gio sub version
然后我编辑了我的glade文件,设置了一个信号事件,并点击了handler

这是我在《铁锈》中的相应逻辑

extern crate gtk;
extern crate gio;    

use gtk::prelude::*;
use gio::prelude::*;

use gtk::{Builder,Window, Button};

use std::env::args;

// the handler
fn on_clicked(param: &[glib::Value]) -> Option<glib::Value> {
    println!("on_start_clicked fired!");
    None
}

fn build_ui(application: &gtk::Application) {
    let glade_src = include_str!("example.glade");
    let builder = Builder::from_string(glade_src);

    let window: Window = builder.get_object("window1").expect("Couldn't get window");
    
    window.set_application(Some(application));
    window.set_title("Test");
    
    window.connect_delete_event(|_, _| {
            gtk::main_quit();
            Inhibit(true)
        });

     // directly calling the button1 without implementing signal
    //let btn: Button = builder.get_object("button1").expect("Cant get button");
    //btn.connect_clicked(|_| {
            //println!("Activated");
        //});


    builder.connect_signals(|builder, handler_name| {
    match handler_name {
        // handler_name as defined in the glade file => handler function as defined above
        "_on_clicked" => Box::new(on_clicked),
        _ => Box::new(|_| {None})
    }
});
    
    window.show_all();
}

fn main() {
    let application = gtk::Application::new(
        Some("com.test.app"),
        Default::default(),
    )
    .expect("Initialization failed...");

    application.connect_activate(|app| {
        build_ui(app);
    });

    application.run(&args().collect::<Vec<_>>());
}

我不能说这是一个聪明/正确的方法,但看看你的错误

油嘴滑舌是它自己的依赖,所以在你的货物。我添加它。 我认为您至少需要:

[dependancies]
gio = {version = "*", features =["v2_44"]}
glib = "*"
gtk = {version = "*", features =["v3_16"]}
gdk = "*"
其中一些功能是我的程序特别需要的

我的使用声明如下:

// imports for GTK UI windows
use gio::prelude::*;
use gtk::prelude::*;
use glib;

use gtk::{Button} // as an example widget
前奏曲应该有助于解决连接和运行ect所缺少的功能

我跑步的主要特点是,与你的相似,但不同:

// gtk UI setup an run
    let application =
        gtk::Application::new(Some("Program name"), Default::default())
            .expect("Initialization failed...");
    
    application.connect_activate(|app| {
        build_ui(app);
    });
    
    application.run(&args().collect::<Vec<_>>());
extern crate gtk;
extern crate gio;    

use gtk::prelude::*;
use gio::prelude::*;

use gtk::{Builder,Window, Button};

use std::env::args;

// the handler
fn on_clicked(param: &[glib::Value]) -> Option<glib::Value> {
    println!("on_start_clicked fired!");
    None
}

fn build_ui(application: &gtk::Application) {
    let glade_src = include_str!("example.glade");
    let builder = Builder::from_string(glade_src);

    let window: Window = builder.get_object("window1").expect("Couldn't get window");
    
    window.set_application(Some(application));
    window.set_title("Test");
    
    window.connect_delete_event(|_, _| {
            gtk::main_quit();
            Inhibit(true)
        });

     // directly calling the button1 without implementing signal
    //let btn: Button = builder.get_object("button1").expect("Cant get button");
    //btn.connect_clicked(|_| {
            //println!("Activated");
        //});


    builder.connect_signals(|builder, handler_name| {
    match handler_name {
        // handler_name as defined in the glade file => handler function as defined above
        "_on_clicked" => Box::new(on_clicked),
        _ => Box::new(|_| {None})
    }
});
    
    window.show_all();
}

fn main() {
    let application = gtk::Application::new(
        Some("com.test.app"),
        Default::default(),
    )
    .expect("Initialization failed...");

    application.connect_activate(|app| {
        build_ui(app);
    });

    application.run(&args().collect::<Vec<_>>());
}
[dependancies]
gio = {version = "*", features =["v2_44"]}
glib = "*"
gtk = {version = "*", features =["v3_16"]}
gdk = "*"
// imports for GTK UI windows
use gio::prelude::*;
use gtk::prelude::*;
use glib;

use gtk::{Button} // as an example widget
// gtk UI setup an run
    let application =
        gtk::Application::new(Some("Program name"), Default::default())
            .expect("Initialization failed...");
    
    application.connect_activate(|app| {
        build_ui(app);
    });
    
    application.run(&args().collect::<Vec<_>>());