使用 Perl 和 GtkBuilder 连接信号

发布于 2024-11-04 15:01:26 字数 787 浏览 5 评论 0原文

我试图弄清楚如何从信号处理程序中访问小部件。

我有一个名为“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-11-11 15:01:26

您需要将要访问的小部件作为“用户数据”参数传递给信号处理程序。在这种情况下,您可以执行类似的操作

$label = $builder->get_object("lblVerify");
$builder->connect_signals($label);

,将标签作为用户数据参数传递给所有信号处理程序。然后传递给 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

$label = $builder->get_object("lblVerify");
$builder->connect_signals($label);

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.)

山色无中 2024-11-11 15:01:26

谢谢番茄。

这就是我将小部件传递给函数所需的。至于函数本身,这是有效的:

sub on_btnSpacewalkVerify_clicked
{

    my $self = shift;
    my $label = shift;

    $label->set_text("Verified");

}   

Thanks ptomato.

That was what I needed to get the widget passed to the function. As for the function itself, this worked:

sub on_btnSpacewalkVerify_clicked
{

    my $self = shift;
    my $label = shift;

    $label->set_text("Verified");

}   
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文