捕获GTK/Rust中Entry输入的逻辑是什么样的?

huangapple go评论58阅读模式
英文:

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) =&gt; {
    console.log(event.target.value);
  };

  return (
    &lt;div&gt;
      &lt;form onSubmit={handleFormSubmit}&gt;
        &lt;input value={term} onChange={handleChange} /&gt;
      &lt;/form&gt;
    &lt;/div&gt;
  );
}

And console log the input provided. Now in the world of GTK I have done something like this:

fn build(&amp;mut self) -&gt; &amp;mut Self {
  .entry(&quot;input_element&quot;)
    .with_entry(|input_element| {
      input_element.set_max_length(MAX_DESCRIPTION_LEN);
    })
  .end(&quot;input_element&quot;)
}

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(&amp;mut self, button: &amp;Button) {
  let name = button.widget_name();
  if name.eq(&quot;button_name_text&quot;) {
    *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`
});

独立工作的示例:
捕获GTK/Rust中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!(&quot;You entered: {:?}&quot;, target.text());
    // `input_element` and `target` refer to the same `Entry`
});

Working standalone example:
捕获GTK/Rust中Entry输入的逻辑是什么样的?

use gtk::prelude::*;

fn main() {
    let application =
        gtk::Application::new(Some(&quot;stackoverflow.questions_75710906&quot;), Default::default());

    application.connect_activate(|application| {
        let window = gtk::ApplicationWindow::new(application);

        let input_element = gtk::Entry::new();

        // ...
        input_element.connect_changed(|target| {
            println!(&quot;You entered: {:?}&quot;, target.text());
            // `input_element` and `target` refer to the same `Entry`
        });
        // ...

        window.add(&amp;input_element);

        window.show_all();
    });

    application.run();
}

huangapple
  • 本文由 发表于 2023年3月12日 11:30:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/75710906.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定