为什么 Gtk2::Builder 会混淆我的信号?

发布于 2024-08-23 07:20:13 字数 4035 浏览 7 评论 0原文

我有这个 perl 代码:

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

use Gtk2 '-init';

my $data;

my $builder_file = "lists.glade";

my $builder = Gtk2::Builder->new();
$builder->add_from_file( $builder_file )
    or die "Couldn't read $builder_file";
$builder->connect_signals( undef );


my $window = $builder->get_object( "top_window" )
    or die "Error while setting up GUI";

$window->show_all();

# Dealloc builder
$builder = undef;

Gtk2->main();

sub top_window_destroy_cb
{
    Gtk2->main_quit();
}

sub on_radiobutton1_group_changed
{
    my ($self, $choice) = @_;

    print Dumper($self, $choice, $choice->get_text());
}

和这个构建器文件:

<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="top_window">
    <signal name="destroy" handler="top_window_destroy_cb"/>
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkVButtonBox" id="vbuttonbox1">
            <property name="visible">True</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkRadioButton" id="radiobutton1">
                <property name="label" translatable="yes">Pera</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">False</property>
                <property name="active">True</property>
                <property name="draw_indicator">True</property>
                <signal name="group_changed" handler="on_radiobutton1_group_changed" object="choice"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkRadioButton" id="radiobutton2">
                <property name="label" translatable="yes">Mika</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">False</property>
                <property name="draw_indicator">True</property>
                <property name="group">radiobutton1</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="choice">
            <property name="visible">True</property>
            <property name="label" translatable="yes">Choice</property>
          </object>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

但它错误地将“on_radiobutton1_group_changed”与来自“top_window”的“destroy”信号连接起来。我知道这一点是因为它仅在关闭窗口时执行“print Dumper...”语句。有谁知道我做错了什么?

I'm having this perl code:

#!/usr/bin/perl

use warnings;
use strict;
use Data::Dumper;

use Gtk2 '-init';

my $data;

my $builder_file = "lists.glade";

my $builder = Gtk2::Builder->new();
$builder->add_from_file( $builder_file )
    or die "Couldn't read $builder_file";
$builder->connect_signals( undef );


my $window = $builder->get_object( "top_window" )
    or die "Error while setting up GUI";

$window->show_all();

# Dealloc builder
$builder = undef;

Gtk2->main();

sub top_window_destroy_cb
{
    Gtk2->main_quit();
}

sub on_radiobutton1_group_changed
{
    my ($self, $choice) = @_;

    print Dumper($self, $choice, $choice->get_text());
}

and this builder file:

<?xml version="1.0"?>
<interface>
  <requires lib="gtk+" version="2.16"/>
  <!-- interface-naming-policy project-wide -->
  <object class="GtkWindow" id="top_window">
    <signal name="destroy" handler="top_window_destroy_cb"/>
    <child>
      <object class="GtkVBox" id="vbox1">
        <property name="visible">True</property>
        <property name="orientation">vertical</property>
        <child>
          <object class="GtkVButtonBox" id="vbuttonbox1">
            <property name="visible">True</property>
            <property name="orientation">vertical</property>
            <child>
              <object class="GtkRadioButton" id="radiobutton1">
                <property name="label" translatable="yes">Pera</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">False</property>
                <property name="active">True</property>
                <property name="draw_indicator">True</property>
                <signal name="group_changed" handler="on_radiobutton1_group_changed" object="choice"/>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">0</property>
              </packing>
            </child>
            <child>
              <object class="GtkRadioButton" id="radiobutton2">
                <property name="label" translatable="yes">Mika</property>
                <property name="visible">True</property>
                <property name="can_focus">True</property>
                <property name="receives_default">False</property>
                <property name="draw_indicator">True</property>
                <property name="group">radiobutton1</property>
              </object>
              <packing>
                <property name="expand">False</property>
                <property name="fill">False</property>
                <property name="position">1</property>
              </packing>
            </child>
          </object>
          <packing>
            <property name="position">0</property>
          </packing>
        </child>
        <child>
          <object class="GtkLabel" id="choice">
            <property name="visible">True</property>
            <property name="label" translatable="yes">Choice</property>
          </object>
          <packing>
            <property name="position">1</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

But it falsely connects "on_radiobutton1_group_changed" with "destroy" signal from "top_window". I know this because it executes "print Dumper..." statement only when closing window. Does anyone has idea what I'm doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

橙幽之幻 2024-08-30 07:20:13

“组已更改”信号

当单选按钮所属的单选按钮组发生更改时发出。当单选按钮从单独切换为一组 2 个或更多按钮的一部分时(反之亦然),以及当按钮从一组 2 个或更多按钮移动到另一个按钮时,会发出此信号,但当按钮所属组的组成发生变化。

听起来确实不是你想要的。我猜它是在破坏窗户时调用的。

我添加了以下子项

sub on_radiobutton_toggled {
    my $self = shift;
    if ($self->get_active) {
        print Dumper($self, $self->get_label);
    }
}

,并将每个单选按钮的切换信号(在 GtkToggleButton 下)设置为 on_radiobutton_toggled,它现在可以执行您想要的操作。

The "group-changed" signal

Emitted when the group of radio buttons that a radio button belongs to changes. This is emitted when a radio button switches from being alone to being part of a group of 2 or more buttons, or vice-versa, and when a button is moved from one group of 2 or more buttons to a different one, but not when the composition of the group that a button belongs to changes.

It really doesn't sound like what you want. I'm guessing it is called on destruction of the window.

I added the following sub

sub on_radiobutton_toggled {
    my $self = shift;
    if ($self->get_active) {
        print Dumper($self, $self->get_label);
    }
}

and set the each radiobutton's toggled signal (under GtkToggleButton) to on_radiobutton_toggled, which now does what you want.

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