使用 Perl 和 GtkBuilder 连接信号
我试图弄清楚如何从信号处理程序中访问小部件。
我有一个名为“lblVerify”的标签,我只想在单击按钮时将文本更改为“已验证”。我知道我需要使用类似 Gtk2::Label->set_text 的东西,但我不完全确定如何从 on_btnVerify_clicked 函数中访问小部件属性。
#!/usr/bin/perl
use strict;
use warnings;
use Glib qw{ TRUE FALSE };
use Gtk2 '-init';
my $builder;
my $window;
# get a new builder object
$builder = Gtk2::Builder->new();
# load the Gtk File from GLADE
$builder->add_from_file( "testglade.xml" )
or die "Error loading GLADE file";
# create the main window
$window = $builder->get_object( "window1" )
or die "Error while creating Main Window";
# connect the event handlers
$builder->connect_signals( undef );
$window->show_all();
$builder = undef;
Gtk2->main();
exit;
sub on_btnVerify_clicked
{
}
I'm trying to figure how to access a widget from within a signal handler.
I've got a label called "lblVerify" that I just want to change the text to "verified" when I click on the button. I know I need to use something like Gtk2::Label->set_text but I'm not entirely sure how to access the widget properties from within the on_btnVerify_clicked function.
#!/usr/bin/perl
use strict;
use warnings;
use Glib qw{ TRUE FALSE };
use Gtk2 '-init';
my $builder;
my $window;
# get a new builder object
$builder = Gtk2::Builder->new();
# load the Gtk File from GLADE
$builder->add_from_file( "testglade.xml" )
or die "Error loading GLADE file";
# create the main window
$window = $builder->get_object( "window1" )
or die "Error while creating Main Window";
# connect the event handlers
$builder->connect_signals( undef );
$window->show_all();
$builder = undef;
Gtk2->main();
exit;
sub on_btnVerify_clicked
{
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将要访问的小部件作为“用户数据”参数传递给信号处理程序。在这种情况下,您可以执行类似的操作
,将标签作为用户数据参数传递给所有信号处理程序。然后传递给
on_btnVerify_clicked
的参数将是按钮本身和标签。 (抱歉有任何错误,我的 Perl 很生疏。)You need to pass the widgets you want to access as a "user data" parameter to the signal handler. In this case, you would do something like
which passes the label as the user data parameter to all your signal handlers. Then the arguments passed to
on_btnVerify_clicked
will be the button itself and the label. (Sorry for any errors, my Perl is quite rusty.)谢谢番茄。
这就是我将小部件传递给函数所需的。至于函数本身,这是有效的:
Thanks ptomato.
That was what I needed to get the widget passed to the function. As for the function itself, this worked: