How do I connect a custom function to the clicked action of a GTK Button?

旧巷老猫 提交于 2019-12-01 21:29:28

You need to pass a reference to the function, rather than the result of the function. So it should be:

button.clicked.connect (clicked_button);

When the button is clicked GTK+ will invoke the clicked_button function with the button as an argument.

The error message invocation of void method not allowed as expression is telling you you are calling (invoking) the method and it has no result (void). Adding parentheses, (), to the end of a function name invokes that function.

Managed to get it working. Here's the code in case others need it:

int main(string[] args) {
    //  Initialise GTK
    Gtk.init(ref args);

    // Configure our window
    var window = new Gtk.Window();
    window.set_default_size(350, 70);
    window.title = "Hello Packaging App";
    window.set_position(Gtk.WindowPosition.CENTER);
    window.set_border_width(12);
    window.destroy.connect(Gtk.main_quit);

    // Create our button
    var button = new Gtk.Button.with_label("Click Me!");
    button.clicked.connect(clicked_button);

    // Add the button to the window
    window.add(button);
    window.show_all();

    // Start the main application loop
    Gtk.main();
    return 0;
}

// Handled the clicking of the button
void clicked_button(Gtk.Button sender) {
    sender.label = "Clicked. Yippee!";
    sender.set_sensitive(false);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!