Kiran Chauhan

Cogito, ergo sum // I think, therefore I am.

Click on Button Doesn't Print Text in Terminal

I was working on the simple application where, as part of intermediate step, I need to print "Button Clicked!" text in terminal when button is clicked. I know that the Gtk.Button has support for clicked signal and I can connect it using .connect() method in following way.

button.clicked.connect(() => {
  stdout.printf("Button Clicked!");
});

But, this was not working as expected! If I click on button, nothing prints in terminal unless I terminate the running application and then I can see the "Button Clicked!" text. If I click on button four times, I was not seeing "Button Clicked" in terminal. But, as soon as I terminate the application, "Button Clicked!" is printed four times. So, it was working but not the way, I expected. The code was correct and I don't see any problem with code.

But then I found this answer on Stack Overflow. The only problem which I had in above code is absence of \n. I added and it works like the way I expected!

Here is the code snippet related to this issue (in case if you're interested).

public class Application : Gtk.Application {
  public Application() {
    Object(
      application_id: "com.github.username.reponame",
      flags: ApplicationFlags.FLAGS_NONE
    );
  }

  protected override void activate() {
    var window = new Gtk.ApplicationWindow(this) {
      default_height = 400,
      default_width = 300,
      title = "Button Click"
    };

    var button = new Gtk.Button.with_label("Click Me!") {
      margin = 12
    };

    button.clicked.connect(() => {
      stdout.printf("Button Clicked!\n");
    });

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

int main(string[] args) {
  var application = new Application();
  return application.run(args);
}



સાહિત્યિક કાર્યક્રમો

  • તા. ૦૨-૧૮ ઓક્ટોબર ૨૦૨૬ - ગાંધી પુસ્તક મહોત્સવ, ગુજરાત વિદ્યાપીઠ, અમદાવાદ
  • તા. ૨૧-૨૫ ઓક્ટોબર ૨૦૨૬ - સંસ્કૃત વાગ્માધુરી 2026, રા. વિ. પાઠક સભાગૃહ, ગુજરાતી સાહિત્ય પરિસદ, આશ્રમ રોડ, અમદાવાદ.
  • તા. ૨૪–૨૫ ઓક્ટોબર ૨૦૨૬ - અમદાવાદ ઇન્ટરનેશનલ લિટરેચર ફેસ્ટિવલ
  • વધુ માહિતી માટે...

વર્ગીકૃત

કડીઓ

સૂચિત લેખો

ચોપડીઓ

જોયેલ