Eka
Eka

Reputation: 15000

How do we build GUI with glade, gtk-rs in rust?

I created a simple GUI with a window,entry box ,label and a button using glade and saved as example.glade in my src directory of my rust project.

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow">
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkFixed" id="windows1">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
          </object>
          <packing>
            <property name="x">182</property>
            <property name="y">146</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="box1">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
          </object>
          <packing>
            <property name="x">68</property>
            <property name="y">45</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">label</property>
          </object>
          <packing>
            <property name="x">321</property>
            <property name="y">44</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

My aim is to create a simple rust application which changes label name when a value is entered in the entry box and submitted by a button click.

I tried to write a rust backend using this example with gtk-rs v0.9.2 . This is my code

use gtk::glib;
use gtk::prelude::*;
use gtk::{ApplicationWindow, Builder, Button, MessageDialog};

use std::env::args;

fn build_ui(application: &gtk::Application) {
    let glade_src = include_str!("example.glade");
    let builder = Builder::from_string(glade_src);
    let window: ApplicationWindow = builder.get_object("window1").expect("Couldn't get window1");

    window.show_all();
}


fn main() {
    let application = gtk::Application::new(
        Some("com.github.gtk-rs.examples.builder_basics"),
        Default::default(),
    )
    .expect("Initialization failed...");

    application.connect_activate(build_ui);

    application.run(&args().collect::<Vec<_>>());
}

When I run this I get below errors

error: extern crate `glib` is private, and cannot be re-exported (error E0365), consider declaring with `pub`
 --> src/main.rs:1:5
  |
1 | use gtk::glib;


error[E0599]: no method named `connect_activate` found for struct `Application` in the current scope
  --> src/main.rs:23:17
   |
23 |       application.connect_activate(build_ui);
   |                   ^^^^^^^^^^^^^^^^ method not found in `Application`


error[E0599]: no method named `run` found for struct `Application` in the current scope
  --> src/main.rs:25:17
   |
25 |       application.run(&args().collect::<Vec<_>>());
   |                   ^^^ method not found in `Application`

How do we build GUI with glade, gtk-rs in rust?

#Cargo.toml
[dependencies]
gtk = "0.9.2"

Upvotes: 1

Views: 4113

Answers (2)

HowMayIBeHelped
HowMayIBeHelped

Reputation: 43

I can't say this is the clever/correct way to do this but looking at your errors.

glib is its own dependancy so in your cargo.toml add it. The minimum i think you need is:

[dependancies]
gio = {version = "*", features =["v2_44"]}
glib = "*"
gtk = {version = "*", features =["v3_16"]}
gdk = "*"

where some of the features i required specifically for my program.

I have my use statements as this:

// imports for GTK UI windows
use gio::prelude::*;
use gtk::prelude::*;
use glib;

use gtk::{Button} // as an example widget

the preludes should help with the missing functions for connecting and running ect.

my main for running is this, similar but not the same as yours:

// gtk UI setup an run
    let application =
        gtk::Application::new(Some("Program name"), Default::default())
            .expect("Initialization failed...");
    
    application.connect_activate(|app| {
        build_ui(app);
    });
    
    application.run(&args().collect::<Vec<_>>());

Upvotes: 1

Eka
Eka

Reputation: 15000

Its real hard to program in rust with its lack of good documents, tutorials or help. I had to comb through many sites and copy-past-edit many example codes to get this thing working. First of all we have to specify a version in cargo.toml file and the version has to be our installed gtk3, gio version. Now my dependencies look some thing like this.

[dependencies]
glib = "0.10.3"

[dependencies.gtk]
version = "0.9.2"
features = ["v3_XX"] // XX our gtk3 sub version

[dependencies.gio]
version = "0.9.1"
features = ["v2_XX"] // XX our gio sub version

Then I edited my glade file to set a signal event with handler _on_clicked

<?xml version="1.0" encoding="UTF-8"?>
<!-- Generated with glade 3.22.1 -->
<interface>
  <requires lib="gtk+" version="3.20"/>
  <object class="GtkWindow" id="window1">
    <property name="can_focus">False</property>
    <child>
      <placeholder/>
    </child>
    <child>
      <object class="GtkFixed">
        <property name="visible">True</property>
        <property name="can_focus">False</property>
        <child>
          <object class="GtkButton" id="button1">
            <property name="label" translatable="yes">button</property>
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
            <property name="receives_default">True</property>
            <signal name="clicked" handler="_on_clicked" swapped="no"/>
          </object>
          <packing>
            <property name="x">182</property>
            <property name="y">146</property>
          </packing>
        </child>
        <child>
          <object class="GtkEntry" id="box1">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">True</property>
          </object>
          <packing>
            <property name="x">68</property>
            <property name="y">45</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="label1">
            <property name="width_request">100</property>
            <property name="height_request">80</property>
            <property name="visible">True</property>
            <property name="can_focus">False</property>
            <property name="label" translatable="yes">label</property>
          </object>
          <packing>
            <property name="x">321</property>
            <property name="y">44</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

This is my corresponding logic in rust

extern crate gtk;
extern crate gio;    

use gtk::prelude::*;
use gio::prelude::*;

use gtk::{Builder,Window, Button};

use std::env::args;

// the handler
fn on_clicked(param: &[glib::Value]) -> Option<glib::Value> {
    println!("on_start_clicked fired!");
    None
}

fn build_ui(application: &gtk::Application) {
    let glade_src = include_str!("example.glade");
    let builder = Builder::from_string(glade_src);

    let window: Window = builder.get_object("window1").expect("Couldn't get window");
    
    window.set_application(Some(application));
    window.set_title("Test");
    
    window.connect_delete_event(|_, _| {
            gtk::main_quit();
            Inhibit(true)
        });

     // directly calling the button1 without implementing signal
    //let btn: Button = builder.get_object("button1").expect("Cant get button");
    //btn.connect_clicked(|_| {
            //println!("Activated");
        //});


    builder.connect_signals(|builder, handler_name| {
    match handler_name {
        // handler_name as defined in the glade file => handler function as defined above
        "_on_clicked" => Box::new(on_clicked),
        _ => Box::new(|_| {None})
    }
});
    
    window.show_all();
}

fn main() {
    let application = gtk::Application::new(
        Some("com.test.app"),
        Default::default(),
    )
    .expect("Initialization failed...");

    application.connect_activate(|app| {
        build_ui(app);
    });

    application.run(&args().collect::<Vec<_>>());
}

Upvotes: 0

Related Questions