2010-02-26 50 views
1

我在這個Perl代碼:爲什麼Gtk2 :: Builder混淆了我的信號?

#!/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()); 
} 

這Builder文件:

<?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」 「消滅」 的信號。我知道這一點,因爲它僅在關閉窗口時執行「print Dumper ...」語句。有沒有人知道我在做什麼錯了?

回答

2

的「基團的改變」信號

當組單選按鈕,一個無線電按鈕所屬的變化觸發。當單選按鈕從單獨切換到成爲一組2個或更多按鈕的組成部分時,或者當按鈕從一組2個或更多按鈕移動到另一個按鈕時發出,而不是當按鈕所屬組的組成變化。

它確實聽起來不像你想要的。我猜它被稱爲破壞窗戶。

添加以下子

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

,並設置(下GtkToggleButton)的每個單選按鈕的觸發信號on_radiobutton_toggled,現在你想要做什麼。

+0

我不能更無知,謝謝。 – XoR 2010-02-27 13:33:21

+0

我喜歡perl-gtk2與C接口有多接近,所以即使所有示例代碼都在C中,devhelp也是非常有用的。 – MkV 2010-02-27 14:29:11