我在我的gkt-rs代码中得到这些错误消息:
error[E0507]: cannot move out of `image`, a captured variable in an `Fn` closure
error[E0382]: borrow of moved value: `window`
我读过this question和this question,但不知道如何将其应用于我的问题。我假设答案涉及RefCell,但无法正确调用。
这是我的代码:
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Box, Button, FileChooserDialog, Image};
const APP_ID: &str = "org.gtk_rs.test";
fn main() {
let app = Application::builder().application_id(APP_ID).build();
app.connect_activate(build_ui);
app.run();
}
fn build_ui(app: &Application) {
let image = Image::builder().build();
let button = Button::builder().label("Press me!").build();
let mbox = Box::builder().build();
mbox.append(&button);
mbox.append(&image);
let window = ApplicationWindow::builder()
.application(app)
.title("My GTK App")
.child(&mbox)
.build();
button.connect_clicked(move |_| {
let file_chooser = FileChooserDialog::new(
Some("Open image"),
Some(&window),
gtk::FileChooserAction::Open,
&[
("_Cancel", gtk::ResponseType::Cancel),
("_Open", gtk::ResponseType::Accept),
], );
file_chooser.connect_response(move |file_chooser, response| {
if response == gtk::ResponseType::Accept {
let file = file_chooser.file().expect("Couldn't get file");
let filename = file.path().expect("Couldn't get file path");
image.set_from_file(Some(filename));
}
file_chooser.destroy();
});
file_chooser.show();
});
window.present();
}
我将非常感谢你的任何建议。
1条答案
按热度按时间rggaifut1#
感谢@Masklinn,text_viewer example找到了答案: