【问题标题】:How can I change button label within its own callback in FLTK?如何在 FLTK 自己的回调中更改按钮标签?
【发布时间】:2021-03-31 09:59:56
【问题描述】:

我想在按下按钮时更改按钮的标签。 但是我收到了这个错误信息。

error[E0505]: cannot move out of `but` because it is borrowed
  --> src/main.rs:24:22
   |
24 |     but.set_callback(move || but.set_label("PRESSED"));
   |     --- ------------ ^^^^^^^ --- move occurs due to use in closure
   |     |   |            |
   |     |   |            move out of `but` occurs here
   |     |   borrow later used by call
   |     borrow of `but` occurs here

error: aborting due to previous error

代码本身

  1 use fltk::*;
  2 
  3 fn main() {
  4 
  5     let app = app::App::default().with_scheme(app::Scheme::Gleam);
  6     let mut win = window::Window::default()
  7         .with_pos(0, 0)
  8         .with_size(300, 300)
  9         .with_label("FLTK_Window");
 10     let mut but = button::Button::default()
 11         .with_pos(0, 0)
 12         .with_size(100, 50)
 13         .center_of(&win)
 14         .with_label("BUTTON");
 15     win.end();
 16     win.show();
 17     
 18     win.set_color(Color::Black);
 19     but.set_color(Color::from_rgb(0, 100, 0));
 20     but.set_label_color(Color::White);
 21     but.set_label_type(LabelType::Normal);
 22     but.set_callback(move || but.set_label("PRESSED"));
 23     app.run().unwrap();
 24 }

我该如何处理这类问题?

【问题讨论】:

    标签: rust fltk


    【解决方案1】:

    除了 frankenapps answer,fltk 有一个重载的 set_callback 方法 (set_callback2),它可以让您访问小部件的可变引用。

    but.set_callback2(|b| b.set_label("PRESSED"));
    

    如果需要,您还可以使用move 捕获外部变量。

    更新: 从 fltk-rs 1.0 版本开始,set_callback 默认捕获 &mut self。

    but.set_callback(|b| b.set_label("PRESSED"));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多