[Glade-users] multiple istance of glade whit perl

On Tue, 2008-04-15 at 20:39 +0200, Antonio Ragagnin wrote:

i'm writing a tool that,when a button is clicked a new glade window is
open, here my code:

sub btnNewMessage_onclick {
 my $newWin = Gtk2::GladeXML->new('gui/wSMS.glade');
 my $newForm = $newWin->get_widget('wSMS');

package callbackSMS;
my $ID;
sub init {         #it's on form.show()

sub btnOk_clicked {
 print $ID;

Ok, everytime i click btnNewMessage, a new window appear. but if i
click btnOk, the value of $ID will be the same for all windows!
it seems that every $newWin share the same package "callbackSMS"
there is some way to separate the packages? or using a class?


Firstly, there is a gtk-perl mailing list full of happy, helpful people.
Subscribe at: http://mail.gnome.org/mailman/listinfo/gtk-perl-list

Secondly, when you go:

sub btnNewMessage_onclick {
 my $newWin = Gtk2::GladeXML->new('gui/wSMS.glade');
 my $newForm = $newWin->get_widget('wSMS');

 ... you're creating your Gtk2::GladeXML object and assigning it to
$newWin but then $newWin goes out of scope straight away ( at the end
of ?btnNewMessage_onclick ). If you want to have multiple ?callbackSMS
objects open and be able to access them from your script, then you
should keep them in an array or something, otherwise when they go out
of scope, the only code that can get at them is code inside the
??callbackSMS package itself.

And lastly, if you want to store a variable attached to a single
instance of an object, you should do it in an OO way. Here's a snippet
from one of our packages, with your random number storage:

use strict;

package forms::posters_priority;

use Gtk2::Pango;
use Glib qw ( TRUE FALSE );

sub new {
    my ( $class, $globals ) = @_;
    my $self;
    $self->{globals} = $globals;
    bless $self, $class;
    $self->{form} = Gtk2::GladeXML->new(
        "posters_priority" );
    $self->{form}->signal_autoconnect_from_package( $self );
    # Store a random number against this instance
    $self->{ID} = rand( 0,1000 );
    return $self;

I think the way you're doing it is storing the random number as a class
variable instead, and as you've found, there's only ONE of these per class.

Oh yeah ... and if you want to do database stuff from gtk-perl, check out my work at:
http://entropy.homelinux.org/axis :)

Daniel Kasak
IT Developer
NUS Consulting Group
Level 5, 77 Pacific Highway
North Sydney, NSW, Australia 2060
T: (+61) 2 9922-7676 / F: (+61) 2
9922 7989
email: dkasak at nusconsulting.com.au

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