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

Hi On clicking a button, I want the "open a file dialog box" to appear. This is what I did. our $cb = $frame->new_ttk__button(-text => "Calculate"); our $filename = $cb->getOpenFile(); Something is wrong with the second line. Please help me out. I am a noob in perl . Thanks

Replies are listed 'Best First'.
Re: Open a file using Tkx package
by zentara (Cardinal) on Nov 19, 2011 at 11:37 UTC
    # your code is: our $cb = $frame->new_ttk__button(-text => "Calculate"); our $filename = $cb->getOpenFile();
    I don't use Tkx, but from basic knowledge of buttons and callbacks, you don't have a callback for the button. Your code should look something like this:
    our $cb = $frame->new_ttk__button( -text => "Calculate", -command => sub{ our $filename = $cb->getOpenFile(); } );
    See Tkx tutorial

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
Re: Open a file using Tkx package
by runrig (Abbot) on Nov 18, 2011 at 22:14 UTC
    Something is wrong with the second line.

    From your detailed description of the error, I conclude that the error is really on line 42. These kinds of errors are always on line 42.

      Could you help me out how to use getOpenFile(); Using my $filename = Tkx::tk__getOpenFile(); also doesn't work.
        works for me
Re: Open a file using Tkx package
by Gangabass (Vicar) on Nov 19, 2011 at 13:28 UTC
    my $btn_source_file = $mw->new_tk__button( -text => "Choose...", -command => sub { get_source_filename(); } ); sub get_source_filename { $source_file = Tkx::tk___getOpenFile(); }
      Hi I tried that I am still not able to run it. Here's what I did. I am using active perl 5.14 Thanks
      use strict; use Tkx; use constant TRUE => 1; use constant FALSE => 0; #Defining UI components. our $iteration = 0; our $mw = Tkx::widget->new("."); $mw->g_wm_title("G7 Fiery."); $mw->g_wm_minsize(300, 300); #our $filename = Tkx::tk__getOpenFile(); our $frame = $mw->new_ttk__frame( -padding => "3 3 12 12"); our $label1 = $mw->new_ttk__label(-text => "Iteration"); our $label2 = $mw->new_ttk__label(-textvariable => \$iteration); our $cb = $frame->new_ttk__button(-text => "Calculate", -command => su +b {calculate();}); our $cb1 = $frame->new_ttk__button(-text => "open", -command => sub {g +et_source_filename();}); #Placing objects . $frame->g_grid(-column => 0, -row => 0, -sticky => "nwes"); $cb->g_grid(-column => 3, -row => 3, -sticky => "w"); $cb1->g_grid(-column => 2, -row => 3, -sticky => "e"); $label1->g_grid(-column => 2, -row => 1, -sticky => "w"); $label2->g_grid(-column => 3, -row => 1, -sticky => "w"); $mw->g_grid_columnconfigure(0, -weight => 1); $mw->g_grid_rowconfigure(0, -weight => 1); foreach (Tkx::SplitList($frame->g_winfo_children)) { Tkx::grid_configure($_, -padx => 5, -pady => 5); } sub calculate() { $iteration ++; } sub get_source_filename { $source_file = Tkx::tk___getOpenFile(); } Tkx::MainLoop;

        $ perl junk Global symbol "$source_file" requires explicit package name at junk li +ne 35. Execution of junk aborted due to compilation errors.

        It works for me every time

        $ perl -MTkx -e " print Tkx::tk___getOpenFile() " C:/dev/temp/junk
        Uncommenting the line $source_file = Tkx::tk___getOpenFile(); and it runs.
Re: Open a file using Tkx package
by johnny_carlos (Scribe) on Nov 18, 2011 at 21:56 UTC
    Are you using "use strict;" and "or die()" ?
      Yes I have use strict;