FAQ - Help and questions

How do I open a window with a button and a text box using egui and eframe?

#egui#eframe#gui#desktoppublished 0 replies0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

Add eframe, implement the eframe App trait on a struct that holds your state, and call run_native. The ui method runs every frame and describes the whole window again from the current state, so there is no widget tree to keep in sync. This thread has a complete program that passed cargo check.

Checked with rustc 1.93.0, Windows 11, eframe 0.35.0 with default-features = false and the default_fonts and glow features; `cargo check` only (the window was not opened or looked at).

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Answer#

egui is an immediate-mode library: every frame your code says "a heading, a text box, a button" and egui draws it and reports what happened (was the button clicked?). Your own struct holds the state, so a click is just an if in the code that draws the button. eframe is the crate that opens the window and runs the loop.

Cargo.tomltoml
[dependencies]
eframe = { version = "0.35", default-features = false, features = ["default_fonts", "glow"] }
src/main.rsrust
use eframe::egui;

#[derive(Default)]
struct App {
    name: String,
    clicks: u32,
}

impl eframe::App for App {
    // Called for every frame: describe the whole window again from the current state.
    fn ui(&mut self, ui: &mut egui::Ui, _frame: &mut eframe::Frame) {
        egui::CentralPanel::default().show(ui, |ui| {
            ui.heading("Hello");
            ui.horizontal(|ui| {
                ui.label("Name:");
                ui.text_edit_singleline(&mut self.name);
            });
            if ui.button("Greet").clicked() {
                self.clicks += 1;
            }
            ui.label(format!("Hello, {}! ({} clicks)", self.name, self.clicks));
        });
    }
}

fn main() -> eframe::Result {
    let options = eframe::NativeOptions::default();
    eframe::run_native("Hello", options, Box::new(|_cc| Ok(Box::new(App::default()))))
}

The program compiled with cargo check on Windows 11. The window itself was not opened for this thread.

How it works#

  • run_native takes the window title, options, and a closure that builds your app once. After that eframe calls ui again whenever something needs redrawing.
  • ui.text_edit_singleline(&mut self.name) edits the string in place; there is no "text changed" callback to wire up.
  • The default-features = false line keeps the dependency list small; the glow feature selects the OpenGL renderer and default_fonts brings the fonts egui needs to show text. Other renderer features exist; see the eframe documentation.

Common mistakes#

  • Doing slow work inside ui. It runs every frame, so a slow call freezes the window. See the thread on background work.
  • Expecting the method signature to stay fixed between eframe versions. The trait changed over time; match the examples to the version in your Cargo.toml.

Why does my egui app use CPU when nothing happens?#

egui redraws when it gets input or when you ask for a repaint. If you call ctx.request_repaint() every frame, it redraws continuously.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No replies yet