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

Hey all,
You may remember I had a node up last night on this question and got a few replies. Its not the same question at all but its on the same script.
To refres your memory I'm in the process of making my own script editor. I.e. A text editor with a few extra functions. But I'm getting heldup on the eazy stuff. I've only a few questions that I can't seem to get at the moment but I'm sure that there'll be more. Thats why I started a new node. What I need to know is When you open a file to make the name appear up near the title.( I can either get just the Title - , or Title - $filename) And when in the open\save window, when you click on an item in the ListBox make the name of it appear in the Entry line named $name.

Heres the code:
use strict; use warnings; use Tk; my $filename; my $ListBox; my $name; my $saveas; my $Listbox; my $mw=MainWindow->new(-title => 'TextEd - ' . $filename); $mw->geometry('800x600'); my $menu_bar = $mw->Frame(); my $rf = $mw->Frame; my $search_mb = $menu_bar->Menubutton('-text' => 'File', '-relief' => 'raised', '-borderwidth' => 2, )->pack('-side' => 'left', '-padx' => 2 ); $search_mb->command('-label' => 'Open', '-accelerator' => 'Ctrl-o', '-underline' => 0, '-command' => \&open_file ); $search_mb->command('-label' => 'Save', '-accelerator' => 'Ctrl+s', '-underline' => 0, '-command' => \&save_file ); $search_mb->command('-label' => 'Close', '-accelerator' => 'Ctrl+x', '-underline' => 0, '-command' => \&close_file ); $search_mb->command('-label' => 'Exit', '-accelerator' => 'Ctrl-q', '-underline' => 0, '-command' => [$mw => 'destroy'] ); my($InputText) = $rf->Scrolled('TextUndo', -height => '1', -width => '1', -scrollbars => 'osoe', ); $menu_bar->pack(-anchor => 'nw'); $rf->pack(qw/-side right -fill both -expand 1/); $InputText->pack(qw/-side top -fill both -expand 1/); MainLoop; sub open_file{ my $open = $mw->Toplevel(-title => 'Open...'); my $tf = $open->Frame; my $bf = $open->Frame; ($ListBox) = $tf->Scrolled('Listbox', -height => '10', -width => '20', -scrollbars => 'e', ); opendir DIR, "."; $ListBox->insert('end', grep { -f $_ && -r $_ } readdir DIR); close DIR; $name = $bf->Entry(-textvariable => \$filename); my $button = $bf->Button( -command => \&load, -text => 'Open'); $tf->pack(-side => 'top'); $bf->pack(-side => 'bottom'); $ListBox->pack(qw/-side left -fill both -expand 1/); $name->pack(-anchor => 's'); $button->pack(-anchor => 'se'); } sub load{ my ($index) = $ListBox->curselection(); $filename = $ListBox->get($index); $InputText->Load( $filename ); (my $script = $0) =~ s,.*(\/|\\),,; } sub save_file{ my $save = $mw->Toplevel(-title => 'Save...'); my $tf = $save->Frame; my $bf = $save->Frame; ($ListBox) = $tf->Scrolled('Listbox', -height => '10', -width => '20', -scrollbars => 'e', ); opendir DIR, "."; $ListBox->insert('end', grep { -f $_ && -r $_ } readdir DIR); close DIR; $name = $bf->Entry(-textvariable => \$filename); my $button = $bf->Button( -command => \&save, -text => 'Save'); $tf->pack(-side => 'top'); $bf->pack(-side => 'bottom'); $ListBox->pack(qw/-side left -fill both -expand 1/); $name->pack(-anchor => 's'); $button->pack(-anchor => 'se'); } sub save{ $InputText->Save( $filename ); } sub entry{ my ($index) = $ListBox->curselection(); $filename = $ListBox->get($index); }
And thats it.
This is my first 'proper' script if you get my meaning and
I would appriciate as much help as possible.
Also if anyone has any suggestions for things to add in or anything like that to make it more efficient or what ever.
Thanks for all the help so far.

All the Best, Eoin...

If everything seems to be going well, you obviously don't know what the hell is going on.

Replies are listed 'Best First'.
Re: Binding an action to an item in a Listbox
by tall_man (Parson) on Apr 17, 2003 at 18:16 UTC
    Here's what you need for the ListBox part:
    # Insert in open_file after $ListBox is defined. $ListBox->bind('<ButtonRelease-1>', sub { $filename = $ListBox->get('active')});
    And here's what you need for the title part:
    # Insert in load after $filename is set. $mw->configure(-title => 'TextEd - ' . $filename);
      That work like a charm tall_man.
      If i need more I'll add to this node so keep your eyes open.
      Thanks,
      Eoin.
Re: Binding an action to an item in a Listbox
by Improv (Pilgrim) on Apr 17, 2003 at 18:10 UTC
    Here's a simple script that has a button that changes the main window's title
    use Tk; my $main = MainWindow->new; $main->Button(-text => 'Redo', -command => sub{$main->title('Moo');})- +>pack; MainLoop;
    As you can see, it's very easy to change the title of a window -- just use the title() method. As for changing the value of that field, use the configure() method on it, changing the text field to the value you want. configure takes similar arguments to the constructor.
Re: Binding an action to an item in a Listbox
by eoin (Monk) on Apr 18, 2003 at 10:08 UTC
    Hey all,
    Thanks for all the help so far.
    Listen I have a new problem. When you go and click on open to open a file the open window pops up. You select a file, its name appears in the entry bar and you click open. Now this loads the file, but the window stays open. How can I get it to close when you click on open as well as loading the sub "load"??
    I tried this, but to no avail.
    $name = $bf->Entry(-textvariable => \$filename); my $button = $bf->Button( -command => \&load, -text => 'Open'); $tf->pack(-side => 'top'); $bf->pack(-side => 'bottom'); $ListBox->pack(qw/-side left -fill both -expand 1/); $name->pack(-anchor => 's'); $button->pack(-anchor => 'se'); } sub load{ my ($index) = $ListBox->curselection(); $filename = $ListBox->get($index); $mw->configure(-title => 'TextEd - ' . $filename); [ $open => 'destroy']; $InputText->Load( $filename ); (my $script = $0) =~ s,.*(\/|\\),,; }
    I also tried this.
    $name = $bf->Entry(-textvariable => \$filename); my $button = $bf->Button( -command => \&load, -command =>[ $open => 'destroy'], -text => 'Open'); $tf->pack(-side => 'top'); $bf->pack(-side => 'bottom'); $ListBox->pack(qw/-side left -fill both -expand 1/); $name->pack(-anchor => 's'); $button->pack(-anchor => 'se'); } sub load{ my ($index) = $ListBox->curselection(); $filename = $ListBox->get($index); $mw->configure(-title => 'TextEd - ' . $filename); $InputText->Load( $filename ); (my $script = $0) =~ s,.*(\/|\\),,; }
    I'm kina running ow on ideas..
    Any suggestions???
    Thanks

    All the Best, Eoin...

    If everything seems to be going well, you obviously don't know what the hell is going on.

      Hi eoin. With pop-up dialogs like this which the user may open and close several times, it's often a good idea to hold onto them and not create and destroy them each time. So if you say this within sub load:
      $open->withdraw();
      And also test in the routine where you create $open:
      if (defined $open) { $open->deiconify(); } else { # create the dialog window here }
      That should do the trick. Make sure $open is a my variable at the top scope level instead of just within the routine, of course.