Set image on button (Gtk)

First of all, I started learning Rust this weekend, so there is a lot I don't understand yet, so go easy on me :slight_smile:

The Monitors struct in the code below just contains information about the number of monitors I have, and resolutions etc (which I get from running xrandr with std::process::Command and parsing the result). This is supposed to be a program where I can easily "turn off" certain monitors for example when watching a movie, without physically have to turn them off.


What I am trying to do now, is to add a screenshot of each monitor on each button to make it look better.

When I am trying to to set an image on a button, Visual Studio Code is giving me a red squiggly line when calling button.set_image(screenshot) with this message:

let screenshot : Option<image::DynamicImage> = match take_screenshot(){ ... }
mismatched types

expected reference, found enum `image::DynamicImage`

note: expected type `std::option::Option<&_>`
         found type `std::option::Option<image::DynamicImage>`rustc(E0308), 26): expected reference, found enum `image::DynamicImage`

I have tried as_ref() which I think should solve this (?):


but this is giving me the message:

the trait bound image::DynamicImage: glib::object::IsA<gtk::auto::widget::Widget> is not satisfied

the trait glib::object::IsA<gtk::auto::widget::Widget> is not implemented for image::DynamicImagerustc(E0277), 16): the trait glib::object::IsA<gtk::auto::widget::Widget> is not implemented for image::DynamicImage

I get the general idea of what it is complaining about, but I have no idea how to solve it. Hopefully someone knows exactly what I am doing wrong here, even if Linux and Gtk is not your expertise.

Obviously, I will need to crop and scale down the image eventually, when this problem is solved, but one step at the time...

Here is the relevant code:

fn setup_form(builder : gtk::Builder, monitors : monitors::Monitors) {
    let monitor_area: gtk::Fixed = builder.get_object("monitor_area").expect("Glade file error: monitor_area!");
    let screenshot : Option<image::DynamicImage> = match take_screenshot(){
        Ok(image) => Some(image),
        Err(_) => None
    for monitor in monitors.items {
        // Create a button for the monitor
        let button = gtk::Button::new();
        // Put it in the right spot
        monitor_area.put(&button, monitor.x/SCALE, monitor.y/SCALE);
        // Resize it to the right size
        button.set_size_request(monitor.width/SCALE, monitor.height/SCALE);

        button.set_image(screenshot); // <- PROBLEM IS HERE!

fn take_screenshot() -> image::ImageResult<image::DynamicImage> {
    let path: &str = "/tmp/softdark.png";


This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.