Gtk.Entry unable to set_text from another method












1















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);
}


}










share|improve this question



























    1















    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);
    }


    }










    share|improve this question

























      1












      1








      1








      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);
      }


      }










      share|improve this question














      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);
      }


      }







      gtk3 vala






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 26 '18 at 21:21









      BoMatBoMat

      103




      103
























          1 Answer
          1






          active

          oldest

          votes


















          3














          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].






          share|improve this answer
























          • Nice job! I knew what the problem was, but couldn't figure out how to fix it in Vala. I was trying but.clicked.connect(tryThis, entry); to pass the entry object to the callback but that wasn't working.

            – theGtknerd
            Nov 26 '18 at 22:44











          • Perfect! That was very well explained. Thank you!

            – BoMat
            Nov 26 '18 at 22:45











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489256%2fgtk-entry-unable-to-set-text-from-another-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          3














          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].






          share|improve this answer
























          • Nice job! I knew what the problem was, but couldn't figure out how to fix it in Vala. I was trying but.clicked.connect(tryThis, entry); to pass the entry object to the callback but that wasn't working.

            – theGtknerd
            Nov 26 '18 at 22:44











          • Perfect! That was very well explained. Thank you!

            – BoMat
            Nov 26 '18 at 22:45
















          3














          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].






          share|improve this answer
























          • Nice job! I knew what the problem was, but couldn't figure out how to fix it in Vala. I was trying but.clicked.connect(tryThis, entry); to pass the entry object to the callback but that wasn't working.

            – theGtknerd
            Nov 26 '18 at 22:44











          • Perfect! That was very well explained. Thank you!

            – BoMat
            Nov 26 '18 at 22:45














          3












          3








          3







          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].






          share|improve this answer













          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].







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 26 '18 at 22:26









          AlThomasAlThomas

          2,587517




          2,587517













          • Nice job! I knew what the problem was, but couldn't figure out how to fix it in Vala. I was trying but.clicked.connect(tryThis, entry); to pass the entry object to the callback but that wasn't working.

            – theGtknerd
            Nov 26 '18 at 22:44











          • Perfect! That was very well explained. Thank you!

            – BoMat
            Nov 26 '18 at 22:45



















          • Nice job! I knew what the problem was, but couldn't figure out how to fix it in Vala. I was trying but.clicked.connect(tryThis, entry); to pass the entry object to the callback but that wasn't working.

            – theGtknerd
            Nov 26 '18 at 22:44











          • Perfect! That was very well explained. Thank you!

            – BoMat
            Nov 26 '18 at 22:45

















          Nice job! I knew what the problem was, but couldn't figure out how to fix it in Vala. I was trying but.clicked.connect(tryThis, entry); to pass the entry object to the callback but that wasn't working.

          – theGtknerd
          Nov 26 '18 at 22:44





          Nice job! I knew what the problem was, but couldn't figure out how to fix it in Vala. I was trying but.clicked.connect(tryThis, entry); to pass the entry object to the callback but that wasn't working.

          – theGtknerd
          Nov 26 '18 at 22:44













          Perfect! That was very well explained. Thank you!

          – BoMat
          Nov 26 '18 at 22:45





          Perfect! That was very well explained. Thank you!

          – BoMat
          Nov 26 '18 at 22:45




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53489256%2fgtk-entry-unable-to-set-text-from-another-method%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

          Calculate evaluation metrics using cross_val_predict sklearn

          Insert data from modal to MySQL (multiple modal on website)