繁体   English   中英

Rust Gtk 使用 Glade 文件从 pixbuf 设置 GtkImage

[英]Rust Gtk set GtkImage from pixbuf using glade file

我正在尝试使用 Rust 将图像从缩放的 pixbuf 加载到 Glade 的现有 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 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");

暂无
暂无

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

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