Click on Button Doesn't Print Text in Terminal
Saturday, November 6, 2021
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); }
---