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

I'm trying to get Gtk2::TextView to display a simple line of text when you hit the Connect button. I cant seem to see why it does not work. EDITED: I added the whole code here and some of the suggestions
use Glib qw/TRUE FALSE/; use Gtk2 '-init'; use strict; use warnings; my $user = "uas"; my $server = "updraft.unl.edu"; my $uas_local = '/home/deadpickle/Desktop/Gtk2'; my $uas_remote = '/home/uas/Scripts/COORDS'; my $way_local = '/home/deadpickle/Desktop/Gtk2'; my $way_remote = '/home/uas/Scripts/WP'; #Main Window my $window = Gtk2::Window->new('toplevel'); $window->signal_connect(destroy => sub { Gtk2->main_quit; }); $window->set_border_width(3); $window->set_title("Virtual Cockpit Control Panel"); #Vbox setup and sub run my $vbox = &ret_vbox(); #add vbox $window->add( $vbox); $window->show; #Main loop Gtk2->main; #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #vbox sub ret_vbox { #create the vbox to pack everything into my $vbox = Gtk2::VBox->new( FALSE, 5); ############################### #Login info my $login_hbox = Gtk2::HBox->new( FALSE, 5); #Frame, Label and Entry my $login_frame = Gtk2::Frame->new("Login"); my $user_lbl = Gtk2::Label->new("User"); my $server_lbl = Gtk2::Label->new("Server"); my $user_eny = Gtk2::Entry->new; my $server_eny = Gtk2::Entry->new; #set entry text, width $user_eny->set_width_chars(5); $user_eny->set_text( $user); $server_eny->set_text( $server); #pack into hbox $login_hbox->pack_start( $user_lbl, FALSE, FALSE, 0); $login_hbox->pack_start( $user_eny, FALSE, FALSE, 0); $login_hbox->pack_start( $server_lbl, FALSE, FALSE, 0); $login_hbox->pack_start( $server_eny, FALSE, FALSE, 0); #add to frame $login_frame->add( $login_hbox); #pack it $vbox->pack_start($login_frame, FALSE, FALSE, 0); ############################### #uas info my $uas_hbox = Gtk2::HBox->new( FALSE, 5); #Frame, Label and Entry my $uas_frame = Gtk2::Frame->new("UAS"); my $uas_local_lbl = Gtk2::Label->new("Local"); my $uas_remote_lbl = Gtk2::Label->new("Remote"); my $uas_local_eny = Gtk2::Entry->new; my $uas_remote_eny = Gtk2::Entry->new; #set entry text $uas_local_eny->set_text( $uas_local); $uas_remote_eny->set_text( $uas_remote); #pack into hbox $uas_hbox->pack_start( $uas_local_lbl, FALSE, FALSE, 0); $uas_hbox->pack_start( $uas_local_eny, FALSE, FALSE, 0); $uas_hbox->pack_start( $uas_remote_lbl, FALSE, FALSE, 0); $uas_hbox->pack_start( $uas_remote_eny, FALSE, FALSE, 0); #add to frame $uas_frame->add( $uas_hbox); #pack it $vbox->pack_start($uas_frame, FALSE, FALSE, 0); ############################### #waypoint info my $way_hbox = Gtk2::HBox->new( FALSE, 5); #Frame, Label and Entry my $way_frame = Gtk2::Frame->new("Waypoints"); my $way_local_lbl = Gtk2::Label->new("Local"); my $way_remote_lbl = Gtk2::Label->new("Remote"); my $way_local_eny = Gtk2::Entry->new; my $way_remote_eny = Gtk2::Entry->new; #set entry text $way_local_eny->set_text( $way_local); $way_remote_eny->set_text( $way_remote); #pack into hbox $way_hbox->pack_start( $way_local_lbl, FALSE, FALSE, 0); $way_hbox->pack_start( $way_local_eny, FALSE, FALSE, 0); $way_hbox->pack_start( $way_remote_lbl, FALSE, FALSE, 0); $way_hbox->pack_start( $way_remote_eny, FALSE, FALSE, 0); #add to frame $way_frame->add( $way_hbox); #pack it $vbox->pack_start($way_frame, FALSE, FALSE, 0); ############################### #text box my $text = Gtk2::TextView->new; my $scroll = Gtk2::ScrolledWindow->new; $scroll->set_policy('automatic', 'automatic'); $scroll->add($text); my $buffer = $text->get_buffer; $text->set_editable(0); $vbox->pack_start($scroll, FALSE, FALSE, 0); ############################### #Connect info my $button = Gtk2::Button->new_with_mnemonic ("_Connect"); $button->signal_connect("clicked" => \&connect); $vbox->pack_start($button, FALSE, FALSE, 0); ############################### #Show all $vbox->show_all; return $vbox; } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #Connect subroutine sub connect { my $buffer = shift; $buffer->set_text( "testing the text box and it must be workin +g"); }
I recieve the errors: *** Can't locate object method "set_text" via package "Gtk2::Button" at grruvibeta.pl line 117. *** ignoring at grruvibeta.pl line 24. The lines that the errors are located at are: $buffer->set_text( "testing the text box and it must be working"); and Gtk2->main;

Replies are listed 'Best First'.
Re: Gtk2 text widget
by quester (Vicar) on Jul 11, 2007 at 07:03 UTC
    It would be easier for us to help you if you gave us more of a clue as to what happened. Specifically, which of the lines in your excerpt was line 116 in the original file? Where and what was the "previous declaration" of $buffer you mentioned?

    The line

    buffer = $text->get_buffer;
    
    needs a "$" in front of buffer; this could be causing your problem. As to whether is really should be
    my $buffer = $text->get_buffer;
    
    I can only guess.

    Also, if you posted all of the connect sub, it is using $buffer as a global. This works (if $buffer is really a global), but passing it as a parameter is more flexible and less prone to attracting errors when making changes to the surrounding code. Like this:

    sub connect { my $buffer = shift; $buffer->set_text( "testing the text box and it must be working"); }
    and you can call it as
    ... connect($buffer) ...
    
    Perl has no problem with using lexical variables of the same name to hold the same value in different subs (or different blocks -- enclosed by curly braces), but some people do find it easier to follow the code if each sub uses a different variable name that is unique throughout the program.
      I believe that I got it to work setting $buffer as a global variable. The next thing is that I want the script to post the text on a new line every time the button is pushed. I cant get it to do that and am requiring the monks help.
      use Glib qw/TRUE FALSE/; use Gtk2 '-init'; use strict; use warnings; my $user = "uas"; my $server = "updraft.unl.edu"; my $uas_local = '/home/deadpickle/Desktop/Gtk2'; my $uas_remote = '/home/uas/Scripts/COORDS'; my $way_local = '/home/deadpickle/Desktop/Gtk2'; my $way_remote = '/home/uas/Scripts/WP'; my $buffer; #Main Window my $window = Gtk2::Window->new('toplevel'); $window->signal_connect(destroy => sub { Gtk2->main_quit; }); $window->set_border_width(3); $window->set_title("Virtual Cockpit Control Panel"); #Vbox setup and sub run my $vbox = &ret_vbox(); #add vbox $window->add( $vbox); $window->show; #Main loop Gtk2->main; #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #vbox sub ret_vbox { #create the vbox to pack everything into my $vbox = Gtk2::VBox->new( FALSE, 5); ############################### #Login info my $login_hbox = Gtk2::HBox->new( FALSE, 5); #Frame, Label and Entry my $login_frame = Gtk2::Frame->new("Login"); my $user_lbl = Gtk2::Label->new("User"); my $server_lbl = Gtk2::Label->new("Server"); my $user_eny = Gtk2::Entry->new; my $server_eny = Gtk2::Entry->new; #set entry text, width $user_eny->set_width_chars(5); $user_eny->set_text( $user); $server_eny->set_text( $server); #pack into hbox $login_hbox->pack_start( $user_lbl, FALSE, FALSE, 0); $login_hbox->pack_start( $user_eny, FALSE, FALSE, 0); $login_hbox->pack_start( $server_lbl, FALSE, FALSE, 0); $login_hbox->pack_start( $server_eny, FALSE, FALSE, 0); #add to frame $login_frame->add( $login_hbox); #pack it $vbox->pack_start($login_frame, FALSE, FALSE, 0); ############################### #uas info my $uas_hbox = Gtk2::HBox->new( FALSE, 5); #Frame, Label and Entry my $uas_frame = Gtk2::Frame->new("UAS"); my $uas_local_lbl = Gtk2::Label->new("Local"); my $uas_remote_lbl = Gtk2::Label->new("Remote"); my $uas_local_eny = Gtk2::Entry->new; my $uas_remote_eny = Gtk2::Entry->new; #set entry text $uas_local_eny->set_text( $uas_local); $uas_remote_eny->set_text( $uas_remote); #pack into hbox $uas_hbox->pack_start( $uas_local_lbl, FALSE, FALSE, 0); $uas_hbox->pack_start( $uas_local_eny, FALSE, FALSE, 0); $uas_hbox->pack_start( $uas_remote_lbl, FALSE, FALSE, 0); $uas_hbox->pack_start( $uas_remote_eny, FALSE, FALSE, 0); #add to frame $uas_frame->add( $uas_hbox); #pack it $vbox->pack_start($uas_frame, FALSE, FALSE, 0); ############################### #waypoint info my $way_hbox = Gtk2::HBox->new( FALSE, 5); #Frame, Label and Entry my $way_frame = Gtk2::Frame->new("Waypoints"); my $way_local_lbl = Gtk2::Label->new("Local"); my $way_remote_lbl = Gtk2::Label->new("Remote"); my $way_local_eny = Gtk2::Entry->new; my $way_remote_eny = Gtk2::Entry->new; #set entry text $way_local_eny->set_text( $way_local); $way_remote_eny->set_text( $way_remote); #pack into hbox $way_hbox->pack_start( $way_local_lbl, FALSE, FALSE, 0); $way_hbox->pack_start( $way_local_eny, FALSE, FALSE, 0); $way_hbox->pack_start( $way_remote_lbl, FALSE, FALSE, 0); $way_hbox->pack_start( $way_remote_eny, FALSE, FALSE, 0); #add to frame $way_frame->add( $way_hbox); $vbox->pack_start($way_frame, FALSE, FALSE, 0); ############################### #text box my $text = Gtk2::TextView->new; my $scroll = Gtk2::ScrolledWindow->new; $scroll->set_policy('automatic', 'automatic'); $scroll->add($text); $buffer = $text->get_buffer; $text->set_editable(0); $vbox->pack_start($scroll, FALSE, FALSE, 0); ############################### #Connect info my $button = Gtk2::Button->new_with_mnemonic ("_Connect"); $button->signal_connect("clicked" => \&connect); $vbox->pack_start($button, FALSE, FALSE, 0); ############################### #Show all $vbox->show_all; return $vbox; } #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #Connect subroutine sub connect { $buffer->set_text( "testing the text box and it must be working\n" +); }
Re: Gtk2 text widget
by zentara (Cardinal) on Jul 11, 2007 at 13:07 UTC
    You really should post fully working examples, because it is dubious how you are using the $vbox. You may need to set your $buffer as a global, or pass it as an arg to a sub.

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