我使用 glade 创建了一个带有窗口、输入框、标签和按钮的简单 GUI,并保存example.glade
在我src
的 rust 项目目录中。
<?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 应用程序,当在输入框中输入值并通过单击按钮提交时,它会更改标签名称。
我尝试使用带有 gtk-rs v0.9.2 的示例编写一个 rust 后端。这是我的代码
use gtk::glib;
use gtk::prelude::*;
use gtk::{ApplicationWindow, Builder, Button, MessageDialog};
use std::env::args;
fn build_ui(application: >k::Application) {
let glade_src = include_str!("example.glade");
let builder = Builder::from_string(glade_src);
let window: ApplicationWindow = builder.get_object("window1").expect("Couldn't get window1");
window.show_all();
}
fn main() {
let application = gtk::Application::new(
Some("com.github.gtk-rs.examples.builder_basics"),
Default::default(),
)
.expect("Initialization failed...");
application.connect_activate(build_ui);
application.run(&args().collect::<Vec<_>>());
}
当我运行它时,我得到以下错误
error: extern crate `glib` is private, and cannot be re-exported (error E0365), consider declaring with `pub`
--> src/main.rs:1:5
|
1 | use gtk::glib;
error[E0599]: no method named `connect_activate` found for struct `Application` in the current scope
--> src/main.rs:23:17
|
23 | application.connect_activate(build_ui);
| ^^^^^^^^^^^^^^^^ method not found in `Application`
error[E0599]: no method named `run` found for struct `Application` in the current scope
--> src/main.rs:25:17
|
25 | application.run(&args().collect::<Vec<_>>());
| ^^^ method not found in `Application`
我们如何在 rust 中使用 glade、gtk-rs 构建 GUI?
#Cargo.toml
[dependencies]
gtk = "0.9.2"