Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

A dynamic Main Window in GTK2

by deadpickle (Pilgrim)
on Dec 20, 2007 at 21:46 UTC ( #658291=perlquestion: print w/replies, xml ) Need Help??

deadpickle has asked for the wisdom of the Perl Monks concerning the following question:

I would like to know if there is a way to edit the contents of the main window in the Gtk2 main loop. What i mean is that after I build the main window and display it (say it contains just a label) the user is then asked whether they want to add a table to the main window. The idea is that if the user says yes then the table is added to the main window and it is re displayed.

Replies are listed 'Best First'.
Re: A dynamic Main Window in GTK2
by traveler (Parson) on Dec 20, 2007 at 23:33 UTC
    Add it as you would outside the loop (using a layout, adding it to a vbox or hbox, whatever). The be sure to show it with a show_all on the main window.
      Ok. Here is some sample code.
      #!/usr/bin/perl -w use strict; use Gtk2 '-init'; use Glib qw/TRUE FALSE/; #shared variables my $active_interface: shared; #create dummy main window my $main_window = Gtk2::Window->new('toplevel'); $main_window->signal_connect(delete_event=> sub{Gtk2->main_quit}); my $label = Gtk2::Label->new('TEST '); my $main_table = Gtk2::Table->new(2, 1, FALSE); $main_table->attach_defaults($label, 0, 1, 0, 1); $main_window->add($main_table); $main_window->show_all; &start_up; Gtk2->main; #The start up askng the user what position they are in sub start_up{ #the User interface select window my $start_up = Gtk2::Window->new('toplevel'); #create table my $job_table = Gtk2::Table->new(3, 1, FALSE); #label my $job_label = Gtk2::Label->new(" Select User Interface +"); #Combobox to select user my $job_select = Gtk2::ComboBox->new_text; $job_select->append_text('Spotter'); $job_select->append_text('Tracker'); $job_select->append_text('VC Base'); $job_select->append_text('Met Base'); $job_select->set_active(0); #add the Ok button my $job_button = Gtk2::Button->new('Start'); #add to table $job_table->attach_defaults( $job_label, 0, 1, 0, 1); $job_table->attach_defaults( $job_select, 0, 1, 1, 2); $job_table->attach_defaults( $job_button, 0, 1, 2, 3); #add widgets $start_up->add($job_table); $start_up->show_all; #capture the interface type to use and display in the main window $job_button->signal_connect('button-press-event' => sub { $active_in +terface = $job_select->get_active_text; my $active_interface_label = +Gtk2::Label($active_interface); $main_table->attach_defaults($active_ +interface_label, 0, 1, 1, 2); $start_up->destroy}); }
      and upon running I receive these errors: Use of inherited AUTOLOAD for non-method Gtk2::Label() is deprecated at GRRUVI-v line 60. *** unhandled exception in callback: *** Can't locate auto/Gtk2/ in @INC (@INC contains: C:/Perl/site/lib C :/Perl/lib .) at line 60 *** ignoring at line 25. So I guess the question is how do I get this to work? Ant ideas?
        I think your problem is that when you create the Label in the callback you do Gtk2::Label($active_interface); and that should be Gtk2::Label->new($active_interface); (of course, you still need to show_all on the main window)

        HTH, --traveler

Re: A dynamic Main Window in GTK2
by zentara (Archbishop) on Dec 21, 2007 at 15:44 UTC
    #!/usr/bin/perl -w use strict; use Gtk2 '-init'; use constant TRUE => 1; use constant FALSE => 0; my $window = Gtk2::Window->new; $window->set_title('Widget Layout'); $window->signal_connect( destroy => sub { Gtk2->main_quit; } ); $window->set_border_width(3); my $vbox = Gtk2::VBox->new( FALSE, 6 ); $window->add($vbox); my $frame = Gtk2::Frame->new('Buttons'); $vbox->pack_start( $frame, TRUE, TRUE, 0 ); $frame->set_border_width(3); my $hbox = Gtk2::HBox->new( FALSE, 6 ); $frame->add($hbox); $hbox->set_border_width(3); my $inc_button = Gtk2::Button->new('_Click Me'); $hbox->pack_start( $inc_button, FALSE, FALSE, 0 ); my $count = 1; my $add_button = Gtk2::Button->new('add_label'); $hbox->pack_start( $add_button, FALSE, FALSE, 0 ); my $rem_button = Gtk2::Button->new('remove_label'); $hbox->pack_start( $rem_button, FALSE, FALSE, 0 ); my $quit_button = Gtk2::Button->new('_Quit'); $hbox->pack_start( $quit_button, FALSE, FALSE, 0 ); $quit_button->signal_connect( clicked => sub { Gtk2->main_quit; } ); my $label = Gtk2::Label->new('Clicked 0 times.'); # has to be done after we've created the label so we can get to it $inc_button->signal_connect( clicked => sub { $label->set_text("Clicked $count times."); $count++; } ); $add_button->signal_connect( clicked => sub { $vbox->pack_start( $label, TRUE, TRUE, 0 ); $label->show } ); $rem_button->signal_connect( clicked => sub { $label->destroy; } ); $window->show_all; Gtk2->main;

    I'm not really a human, but I play one on earth. Cogito ergo sum a bum

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://658291]
Approved by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2022-10-05 06:40 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (20 votes). Check out past polls.