英文:
What does the logic look like to capture input on Entry with GTK/Rust?
问题
我是一个学习使用Rust的GTK版本3的React开发人员。我现在正在使用Entry,它相当于HTML输入元素。
在React中,我们有一个输入元素上的value属性,你可以通过event.target.value来提取它在浏览器中的值。
在使用GTK/Rust的Entry时,它是如何工作的呢?
再次强调,在React中,我可以这样做:
const handleChange = (event) => {
  console.log(event.target.value);
};
return (
  <div>
    <form onSubmit={handleFormSubmit}>
      <input value={term} onChange={handleChange} />
    </form>
  </div>
);
然后,将输入的值记录到控制台。现在,在GTK的世界中,我已经做了类似这样的事情:
fn build(&mut self) -> &mut Self {
  .entry("input_element")
    .with_entry(|input_element| {
      input_element.set_max_length(MAX_DESCRIPTION_LEN);
    })
  .end("input_element")
}
是否有一种方法可以在entry中记录或读取输入的值,以验证它是否接收到用户输入?
我知道在GTK Rust中也有事件处理,比如处理按钮事件:
fn handle_change(&mut self, button: &Button) {
  let name = button.widget_name();
  if name.eq("button_name_text") {
    *self.button_name_text.borrow_mut() = button.clone();
  }
}
如果还有事件处理来捕获输入的内容,那么相关的逻辑会是什么样子呢?
英文:
I am a React developer that is learning GTK version 3 with Rust. I am now working with Entry which is the equivalent to HTML input elements.
In React we have a value prop on an input element and you can extract in the browser via event.target.value.
How does that work when working with GTK/Rust Entry?
Again, in React I can do something like this:
const handleChange = (event) => {
    console.log(event.target.value);
  };
  return (
    <div>
      <form onSubmit={handleFormSubmit}>
        <input value={term} onChange={handleChange} />
      </form>
    </div>
  );
}
And console log the input provided. Now in the world of GTK I have done something like this:
fn build(&mut self) -> &mut Self {
  .entry("input_element")
    .with_entry(|input_element| {
      input_element.set_max_length(MAX_DESCRIPTION_LEN);
    })
  .end("input_element")
}
Is there a way to console log or read a value typed into the entry to verify it's receiving user input?
I know there is also event handling in GTK Rust with something like a button like so:
fn handle_change(&mut self, button: &Button) {
  let name = button.widget_name();
  if name.eq("button_name_text") {
    *self.button_name_text.borrow_mut() = button.clone();
  }
}
If there is also event handling to capture the input entered, what would that logic look like?
答案1
得分: 1
你可以使用 EditableSignals::connect_changed。
Entry 实现了 EditableSignals 特性,因此它具有 connect_changed 方法。
要获取文本,您可以使用 text 方法,通过扩展特性 EntryExt 提供给 Entry。
您可以使用 println!() 宏进行打印。
例如:
input_element.connect_changed(|target| {
    println!("You entered: {:?}", target.text());
    // `input_element` 和 `target` 指的是相同的 `Entry`
});
use gtk::prelude::*;
fn main() {
    let application =
        gtk::Application::new(Some("stackoverflow.questions_75710906"), Default::default());
    application.connect_activate(|application| {
        let window = gtk::ApplicationWindow::new(application);
        let input_element = gtk::Entry::new();
        // ...
        input_element.connect_changed(|target| {
            println!("You entered: {:?}", target.text());
            // `input_element` 和 `target` 指的是相同的 `Entry`
        });
        // ...
        window.add(&input_element);
        window.show_all();
    });
    application.run();
}
英文:
You can use EditableSignals::connect_changed.
Entry implements the EditableSignals trait, so it has the connect_changed method.
To get the text, you use the text method which Entry has via the extension trait EntryExt.
You can print using the println!() macro.
For example:
input_element.connect_changed(|target| {
    println!("You entered: {:?}", target.text());
    // `input_element` and `target` refer to the same `Entry`
});
use gtk::prelude::*;
fn main() {
    let application =
        gtk::Application::new(Some("stackoverflow.questions_75710906"), Default::default());
    application.connect_activate(|application| {
        let window = gtk::ApplicationWindow::new(application);
        let input_element = gtk::Entry::new();
        // ...
        input_element.connect_changed(|target| {
            println!("You entered: {:?}", target.text());
            // `input_element` and `target` refer to the same `Entry`
        });
        // ...
        window.add(&input_element);
        window.show_all();
    });
    application.run();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。



评论