Re: Binding Variables to Widgets




On Jan 4, 2007, at 12:12 AM, Nik Ogura wrote:

Forgive my noobishness, I'm coming from a Perl/Tk background.

In Perl/Tk you can easily bind a variable to a widget, so that any
changes to the widget text are automatically changed in the bound
variable.

Is this sort of thing possible in Gtk2-perl?  Or am I stuck doing
$widget->set_text and $widget->get_text calls to do the same?

Thank you in advance.

I second Emmanuele's recommendation of Glib::Object::tie_properties (). The thread introducing and debating the relative merits of this mechanism can be found here:

http://mail.gnome.org/archives/gtk-perl-list/2003-October/ msg00201.html

Of course, you could just use the object property interface directly:

   ($foo, $bar, $baz) = $object->get (qw/foo bar baz/);
   $object->set (foo => $new_foo, bar => $new_bar, baz => $new_baz);
   $object->signal_connect ('notify::foo' => \&on_foo_changed);


However, in the spirit of TIMTOWDI and full disclosure, this topic has been broached before. Bjarne Steinsbo created a class called Gtk2::BindVariable, which is discussed in the thread "A little tie magic", which starts here:

http://mail.gnome.org/archives/gtk-perl-list/2003-December/ msg00039.html

We did not include that in the distribution for a few reasons: different developers had style disagreements over it, we already had Glib::Object::tie_properties(), and about this time we realized that including lots of pure-perl code with the xs-only bindings was starting to cause maintenance grief.


Take-home message --- gtk2-perl provides you the full gtk2 API, with a few spartan niceties, but doesn't intend to go beyond that. If you want to layer your own convenience stuff atop that, please do, and share it on CPAN.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]