如何将自定义函数连接到GTK Button的点击操作? [英] How do I connect a custom function to the clicked action of a GTK Button?

查看:499
本文介绍了如何将自定义函数连接到GTK Button的点击操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Elementary OS提供的Vala GTK + 3教程。我知道这段代码:

  var button_hello = new Gtk.Button.with_label(Click me!); 
button_hello.clicked.connect(()=> {
button_hello.label =Hello World!;
button_hello.set_sensitive(false);
});

使用Lambda函数在单击按钮时更改按钮的标签。我想要做的就是调用这个函数:

  void clicked_button(Gtk.Button sender){
sender。标签=Clicked。Yippee!;
sender.set_sensitive(false);
}

我试过这个:

  button.clicked.connect(clicked_button(button)); 

但是当我尝试编译时,我从Vala编译得到这个错误:

  hello-packaging.vala:16.25-16.46:error:调用void方法不允许作为表达式
button.clicked.connect(clicked_button按钮));
^^^^^^^^^^^^^^^^^^^^^^
编译失败:1个错误,0个警告

我是Vala和Linux的新手,所以请保持温柔,但是有人能指出我正确的方向吗?

解决方案

您需要传递函数的引用,而不是函数的结果。所以它应该是:
$ b $ p $ button button.clicked.connect(clicked_button);



点击按钮时,GTK +会将按钮作为参数调用 clicked_button 函数。



错误消息调用void method不允许作为expression 告诉你你正在调用(调用)该方法,并且它没有结果(void)。在函数名称的末尾添加括号(),可以调用该函数。


I am working my way through the Vala GTK+3 tutorial provided by Elementary OS. I understand that this code:

var button_hello = new Gtk.Button.with_label ("Click me!");
button_hello.clicked.connect (() => {
    button_hello.label = "Hello World!";
    button_hello.set_sensitive (false);
});

uses a Lambda function to change the button's label when it's clicked. What I want to do is call this function instead:

void clicked_button(Gtk.Button sender) {
    sender.label = "Clicked. Yippee!";
    sender.set_sensitive(false);
}

I've tried this:

button.clicked.connect(clicked_button(button));

But I get this error from the Vala compile when I try to compile:

hello-packaging.vala:16.25-16.46: error: invocation of void method not allowed as expression
    button.clicked.connect(clicked_button(button));
                           ^^^^^^^^^^^^^^^^^^^^^^
Compilation failed: 1 error(s), 0 warning(s)

I'm new to both Vala and Linux so please be gentle but can someone point me in the right direction?

解决方案

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.

这篇关于如何将自定义函数连接到GTK Button的点击操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆