Search code examples
gtk3vala

Gtk.Entry unable to set_text from another method


Pretty new to vala and Gtk. I am unable to call Gtk.Entry's set_text method, to set text from another method. Here is the sample code which I tried. I am able to set_text while in the activate() method, but just not from the tryThis() method.

using Gtk;
public class MyApplication : Gtk.Application {

public MyApplication () {
    Object(application_id: "testing.my.application",
    flags: ApplicationFlags.FLAGS_NONE);
}

protected override void activate () {

    Gtk.ApplicationWindow window = new Gtk.ApplicationWindow (this);
    window.set_default_size (800, 600);
    window.window_position = WindowPosition.CENTER;
    window.set_border_width(10);

    Gtk.HeaderBar headerbar = new Gtk.HeaderBar();
    headerbar.show_close_button = true;
    headerbar.title = "Window";
    window.set_titlebar(headerbar);

    //Entry is initialized here
    Gtk.Entry entry = new Gtk.Entry();
    entry.set_text ("Before button click");

    //Button is initialized and connect to method
    Gtk.Button but = new Gtk.Button.with_label("Click me");
    but.clicked.connect(tryThis);

    Gtk.Box vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
    vbox.pack_start(entry, false, false, 10);
    vbox.pack_start(but, false, false, 20);

    window.add(vbox);
    window.show_all ();
}

private void tryThis() {
    Gtk.Entry entry = new Gtk.Entry();
    //This is not working!!
    entry.set_text ("After button click");
    message("%s -", "I am here");
}

public static int main (string[] args) {
    MyApplication app = new MyApplication ();
    return app.run (args);
}

}


Solution

  • The problem is a problem of scope. So activate creates an entry within the scope of that method, not the whole class. tryThis creates a new instance of Gtk.Entry and assigns it to a variable entry in the scope of that method, not the whole class.

    This example fixes your problem, but is not the best solution, as discussed after the example:

    using Gtk;
    
    public class MyApplication : Gtk.Application {
    
      Gtk.Entry entry;
    
      public MyApplication () {
        Object(application_id: "testing.my.application",
        flags: ApplicationFlags.FLAGS_NONE);
      }
    
      protected override void activate () {
    
      Gtk.ApplicationWindow window = new Gtk.ApplicationWindow (this);
      window.set_default_size (800, 600);
      window.window_position = WindowPosition.CENTER;
      window.set_border_width(10);
    
      Gtk.HeaderBar headerbar = new Gtk.HeaderBar();
      headerbar.show_close_button = true;
      headerbar.title = "Window";
      window.set_titlebar(headerbar);
    
      //Entry is initialized here
      entry = new Gtk.Entry();
      entry.set_text ("Before button click");
    
      //Button is initialized and connect to method
      Gtk.Button but = new Gtk.Button.with_label("Click me");
      but.clicked.connect(tryThis);
    
      Gtk.Box vbox = new Gtk.Box(Gtk.Orientation.VERTICAL, 0);
      vbox.pack_start(entry, false, false, 10);
      vbox.pack_start(but, false, false, 20);
    
      window.add(vbox);
      window.show_all ();
      }
    
      private void tryThis() {
        entry.set_text ("After button click");
        message("%s -", "I am here");
      }
    
      public static int main (string[] args) {
        MyApplication app = new MyApplication ();
        return app.run (args);
      }
    }
    

    You should notice that:

    • entry is brought in to the scope of the whole class with Gtk.Entry entry; at the beginning of the class definition
    • entry = new Gtk.Entry (); has been removed from tryThis because the entry has already been instantiated when activate is called

    This works, but for the longer term it is better to separate the window from the application. So use activate to instantiate a new MainApplicationWindow for example. Also Vala includes code generation routines for Gtk templates. This allows you to define the window and its child widgets using XML or the GUI tool Glade and then attach the Vala code with the Vala attributes [GtkTemplate], [GtkChild] and [GtkCallback].