Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何在单击按钮事件中执行其他任务时关闭主窗口?_Rust_Gtk - Fatal编程技术网

Rust 如何在单击按钮事件中执行其他任务时关闭主窗口?

Rust 如何在单击按钮事件中执行其他任务时关闭主窗口?,rust,gtk,Rust,Gtk,我在试着重新创造。Python版本使用PyQt5,而Ruby版本使用GTK。当使用Rust或Ruby时,我不知道当用户按下“Tweet”时如何正确执行“Tweet”代码 用户按下的按钮称为“发送”,按下时会调用此代码: send.connect_clicked(move |_| { let sent: Option<String> = TextBuffer::get_text( &TextView::get_buffer(&text).unwr

我在试着重新创造。Python版本使用PyQt5,而Ruby版本使用GTK。当使用Rust或Ruby时,我不知道当用户按下“Tweet”时如何正确执行“Tweet”代码

用户按下的按钮称为“发送”,按下时会调用此代码:

send.connect_clicked(move |_| {
    let sent: Option<String> = TextBuffer::get_text(
        &TextView::get_buffer(&text).unwrap(),
        &TextBuffer::get_start_iter(&TextView::get_buffer(&text).unwrap()), 
        &TextBuffer::get_end_iter(&TextView::get_buffer(&text).unwrap()), false);
    let actual = sent.unwrap();
    println!("Text: {}", actual);
    let toot = Command::new("t").output()
        .expect("Nope");
    println!("{}", String::from_utf8_lossy(&toot.stdout));
    Notification::new().summary("Sent")
        .body(&actual).show().unwrap();
    gtk::main_quit();
});
send.connect_单击(移动| | |{
let sent:Option=TextBuffer::get_text(
&TextView::获取缓冲区(&text).unwrap(),
&TextBuffer::get_start_iter(&TextView::get_buffer(&text).unwrap()),
&TextBuffer::get_end_iter(&TextView::get_buffer(&text).unwrap()),false);
let actual=sent.unwrap();
println!(“文本:{}”,实际);
让toot=Command::new(“t”).output()
.预期(“不”);
println!(“{}”,字符串::from_utf8_lossy(&toot.stdout));
通知::新建()摘要(“已发送”)
.body(&actual).show().unwrap();
gtk::main_quit();
});
但是,每当用户按下它时,窗口就会冻结,直到代码运行完毕,然后主窗口退出


如何正确关闭窗口并执行此代码?它可能非常简单,但我几乎没有对函数进行过适当的研究。编写Python脚本的坏习惯。

为了正确关闭主窗口并执行其他任务,我使用glib的
idle\u add

thread::spawn(move || {
    glib::idle_add(move || {
        toot(tweet);
        gtk::main_quit();
        Continue(false)
    });
});

回调中的代码可能花费的时间太长,UI会冻结直到完成,然后Gtk可以继续并关闭窗口。也许你应该使用线程或异步方法。@JoséFonte我知道如何正确地使用GTK的idle_add的线程。显然不再需要生成线程。