Gtk-rs way to position a button at an x,y coordinate example?

I am new to Rust and gtk-rs. I made a simple app with buttons using information from the gtk-rs online book. I would like to position a button or other "child" using x,y coordinates inside the application window. I have seen in other documentation about using x,y coordinates, but don't know how to get started on using it in my simple app. Is there a code snippet someone can point me to that uses x,y coordinates to position buttons, etc. in a real world example?

You can use Gtk.Fixed to do so. Be sure to read the documentation, which explains why "you should not use this".

Did you read the documentation? Do you want to create broken applications? Sure, let's create broken applications. This should put "Hello, world!" button at x=50.0 and y=100.0.

use gtk4 as gtk;
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button, Fixed};

fn main() {
    let app = Application::builder().build();
    app.connect_activate(|app| {
        let button = Button::with_label("Hello, world!");
        let fixed = Fixed::new();
        fixed.put(&button, 50.0, 100.0);
        let window = ApplicationWindow::builder().application(app).build();
        window.set_child(Some(&fixed));
        window.show();
    });
    app.run();
}

Hope this help,

2 Likes

Thanks sanxiyn. I am starting from the beginning and have alot to learn. I read the documentation on Gtk.Fixed and can see how it would break an app. I ran your code snippet fine and will learn from it. I am looking at ways to arrange many buttons in a window without overlap or breaking, so, I will keep reading. Thanks for your advice!