0

我正在尝试将图像从缩放的 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: &gtk::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 enumstd::result::Result。我正在使用安装了最新 Rust 的 Linux。

4

1 回答 1

1

这是解决方案。更改此部分:

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");
于 2020-02-14T06:49:52.567 回答