hakim-djz has asked for the wisdom of the Perl Monks concerning the following question:

Hi all,

I am trying to develop a Gui on Glade and building it with Perl. I've been following this tutorial http://blog.borovsak.si/2009/09/glade3-tutorial-1-introduction.html where I reached as far as the second part as I could not compile it in C or Perl.

When building with Perl I tried the following which I found from https://github.com/dave-theunsub/gtk3-perl-demos/blob/master/builder.pl

#!/usr/bin/perl use strict; use warnings; use Gtk3 '-init'; #use File::Basename 'dirname'; my $builder = Gtk3::Builder->new(); $builder->add_from_file("/home/hakim/project/tut.ui"); my $window = $builder->get_object('window1'); $builder->connect_signals( undef ); $window->set_screen( $window->get_screen() ); $window->signal_connect( destroy => sub { Gtk3->main_quit } ); $window->show_all(); Gtk3->main(); sub about_activate { my $about_dialog = $builder->get_object('aboutdialog1'); $about_dialog->run(); $about_dialog->hide(); } sub quit_activate { my $action = shift; my $window = $builder->get_object('window1'); $window->destroy; }

Can't call method "get_screen" on an undefined value at tut.pl line 21 is the error message I get from running it. If anyone can help it would be great

Replies are listed 'Best First'.
Re: Can't call method "get_screen" on an undefined value
by kennethk (Abbot) on Mar 06, 2014 at 15:44 UTC

    First, I note that the phrase get_screen does not appear on line 21. This means that the error message you are reporting cannot come from your posted code. If you'd like help debugging, please post the code you are actually running when the error message is issued. Also please wrap error messages in <code> tags, just to make sure nothing gets mangled. Please see How do I post a question effectively?.

    Errors of this type are issued when you invoke a method on a variable that is currently undefined. I'm guessing that since the phrase get_screen only appears on line 14 with $window, $window is undefined. This probably means your initialization call on line 11 failed. So I'd start there.


    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      Thanks for the advice. I realised I had the wrong names set from Glade. So instead of 'window1' I had 'Retro-Fi'. So just changed them and it's all working fine. Thanks again
Re: Can't call method "get_screen" on an undefined value
by zentara (Cardinal) on Mar 07, 2014 at 11:18 UTC
    I'm guessing that you can't call get_screen until the mainwindow is actually mapped to the screen, or is visible. Your window does not become visible until the show_all() command.

    There are probably a couple of ways to deal with it. One is to put the get_screen after the show_all, possibly with a short timer, but a better way would be to find out how to test for visibility, by seeing if the $mw is mapped.

    I don't have an example handy, but google for how to detect if a Gtk3 window is mapped, or present. Try finding the event for window_is_mapped or present(). Then you could use the is_mapped callback to run get_screen().


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Can't call method "get_screen" on an undefined value
by zentara (Cardinal) on Mar 08, 2014 at 19:22 UTC
    Hi, just to be complete, here are a few Gtk2 version scripts for connecting to the map_event.
    #!/usr/bin/perl use warnings; use strict; use Gtk2 -init; my $window = Gtk2::Window->new; $window->add( Gtk2::Label->new("Yaada yadda yadda") ); print "BEFORE mapped ?-> ",$window->mapped,"\n"; $window->show_all; $window->signal_connect( delete_event => sub { exit }); #$window->hide_all; print "AFTER mapped ?-> ",$window->mapped,"\n"; Gtk2->main;

    And to connect to the signal

    #!/usr/bin/perl use warnings; use strict; use Glib qw/TRUE FALSE/; use Gtk2 '-init'; my $window = Gtk2::Window->new('toplevel'); $window->set_title('Z'); $window ->signal_connect( 'destroy' => \&delete_event ); $window->set_border_width(10); $window->set_size_request(300,200); my $vbox = Gtk2::VBox->new( FALSE, 6 ); $window->add($vbox); $vbox->set_border_width(2); my $hbox= Gtk2::HBox->new( FALSE, 6 ); $vbox->pack_end($hbox,FALSE,FALSE,0); $hbox->set_border_width(2); my $button = Gtk2::Button->new_from_stock('gtk-quit'); $hbox->pack_end( $button, FALSE, FALSE, 0 ); $button->signal_connect( clicked => \&delete_event ); $window->set_position('center'); ############################################## # put your screen code in the following callback $window->signal_connect(map_event => sub { print "mapped\n"} ); ############################################### $window->show_all(); Gtk2->main; ##################################### sub delete_event { Gtk2->main_quit; return FALSE; }

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh