Rust Gtk 使用 glade 文件从 pixbuf 设置 GtkImage
Rust Gtk set GtkImage from pixbuf using glade file
我正在尝试将图像从缩放的 pixbuf 加载到 Glade with Rust 的现有 GtkImage 小部件。我已经阅读了所有关于这个主题的 Gtk-rs 文档,它应该基于那个来工作。然而,当尝试从 pixbuf 加载时总是有问题。这是我重现错误的代码:
extern crate gtk;
extern crate gio;
extern crate gdk_pixbuf;
use gtk::prelude::*;
use gio::prelude::*;
use gtk::{Builder, MessageDialog, Window, ButtonsType, DialogFlags, MessageType, ResponseType, Image, ImageExt};
use gdk_pixbuf::{Pixbuf};
use std::env::args;
fn build_ui(application: >k::Application) {
let glade_src = include_str!("test.glade");
let builder = Builder::new_from_string(glade_src);
let window: Window = builder.get_object("main").expect("Couldn't get main");
let img2: Image = builder.get_object("img2").expect("Cangt get img2");
window.set_application(Some(application));
window.set_title("HLearn - 2020 - Tatyó");
window.connect_delete_event(move |_, _| {
println!("Hey");
let window2: Window = builder.get_object("main").expect("Cant get 2nd");
let dialog = MessageDialog::new(Some(&window2),
DialogFlags::MODAL,
MessageType::Question,
ButtonsType::YesNo,
"Are you sure to exit");
dialog.set_title("Attention!");
dialog.set_position(gtk::WindowPosition::__Unknown(0));
let res = dialog.run();
if res == ResponseType::Yes {
println!("Yes");
dialog.destroy();
Inhibit(false)
} else {
println!("No");
dialog.destroy();
Inhibit(true)
}
});
let builder2 = Builder::new_from_string(glade_src);
let window3: Window = builder2.get_object("main").expect("Cant get 3rd");
let (sx, sy) = window3.get_size();
let pixbuf = Pixbuf::new_from_file_at_scale(
"/home/daniel/RUST/test/src/test.gif",
600,
700,
false);
img2.set_from_pixbuf(Some(&pixbuf));
window.show_all();
}
fn main() {
let application = gtk::Application::new(
Some("com.github.swanux.hlearn"),
Default::default(),
)
.expect("Initialization failed...");
application.connect_activate(|app| {
build_ui(app);
});
application.run(&args().collect::<Vec<_>>());
}
这是空地文件 test.glade
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="main">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkImage" id="img2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="stock">gtk-missing-image</property>
</object>
</child>
</object>
</interface>
当我尝试从 pixbuf 加载图像时,我总是收到此错误消息:expected struct gdk_pixbuf::Pixbuf
,found enum std::result::Result
。我正在使用安装了最新 Rust 的 Linux。
这是解决方案。更改此部分:
let pixbuf = Pixbuf::new_from_file_at_scale(
"/home/daniel/RUST/test/src/test.gif",
600,
700,
false);
至
let pixbuf = Pixbuf::new_from_file_at_scale(
"/home/daniel/RUST/test/src/test.gif",
600,
700,
false).expect("Can't get pixbuf");
我正在尝试将图像从缩放的 pixbuf 加载到 Glade with Rust 的现有 GtkImage 小部件。我已经阅读了所有关于这个主题的 Gtk-rs 文档,它应该基于那个来工作。然而,当尝试从 pixbuf 加载时总是有问题。这是我重现错误的代码:
extern crate gtk;
extern crate gio;
extern crate gdk_pixbuf;
use gtk::prelude::*;
use gio::prelude::*;
use gtk::{Builder, MessageDialog, Window, ButtonsType, DialogFlags, MessageType, ResponseType, Image, ImageExt};
use gdk_pixbuf::{Pixbuf};
use std::env::args;
fn build_ui(application: >k::Application) {
let glade_src = include_str!("test.glade");
let builder = Builder::new_from_string(glade_src);
let window: Window = builder.get_object("main").expect("Couldn't get main");
let img2: Image = builder.get_object("img2").expect("Cangt get img2");
window.set_application(Some(application));
window.set_title("HLearn - 2020 - Tatyó");
window.connect_delete_event(move |_, _| {
println!("Hey");
let window2: Window = builder.get_object("main").expect("Cant get 2nd");
let dialog = MessageDialog::new(Some(&window2),
DialogFlags::MODAL,
MessageType::Question,
ButtonsType::YesNo,
"Are you sure to exit");
dialog.set_title("Attention!");
dialog.set_position(gtk::WindowPosition::__Unknown(0));
let res = dialog.run();
if res == ResponseType::Yes {
println!("Yes");
dialog.destroy();
Inhibit(false)
} else {
println!("No");
dialog.destroy();
Inhibit(true)
}
});
let builder2 = Builder::new_from_string(glade_src);
let window3: Window = builder2.get_object("main").expect("Cant get 3rd");
let (sx, sy) = window3.get_size();
let pixbuf = Pixbuf::new_from_file_at_scale(
"/home/daniel/RUST/test/src/test.gif",
600,
700,
false);
img2.set_from_pixbuf(Some(&pixbuf));
window.show_all();
}
fn main() {
let application = gtk::Application::new(
Some("com.github.swanux.hlearn"),
Default::default(),
)
.expect("Initialization failed...");
application.connect_activate(|app| {
build_ui(app);
});
application.run(&args().collect::<Vec<_>>());
}
这是空地文件 test.glade
<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
<requires lib="gtk+" version="3.20"/>
<object class="GtkWindow" id="main">
<property name="can_focus">False</property>
<child>
<placeholder/>
</child>
<child>
<object class="GtkImage" id="img2">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="hexpand">True</property>
<property name="vexpand">True</property>
<property name="stock">gtk-missing-image</property>
</object>
</child>
</object>
</interface>
当我尝试从 pixbuf 加载图像时,我总是收到此错误消息:expected struct gdk_pixbuf::Pixbuf
,found enum std::result::Result
。我正在使用安装了最新 Rust 的 Linux。
这是解决方案。更改此部分:
let pixbuf = Pixbuf::new_from_file_at_scale(
"/home/daniel/RUST/test/src/test.gif",
600,
700,
false);
至
let pixbuf = Pixbuf::new_from_file_at_scale(
"/home/daniel/RUST/test/src/test.gif",
600,
700,
false).expect("Can't get pixbuf");