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

Hey all, I'm in the process of making my own script editor. When finished it'll have debug funcs. etc, but its still in its early stages. I'm having a problem with the menubar. I can't get it to load its new windows. E.g. If i click on Open in the File menu, the window for opening a new file should pop up.
Guess what... It doesn't.
Heres the error I get when I click on Open.
Assuming 'require Tk::TopLevel;' at txt.pl line 75 Subroutine Tk_cmd redefined at C:/Perl/site/lib/Tk/TopLevel.pm line 14 + (#2) (W redefine) You redefined a subroutine. To suppress this warning +, say { no warnings 'redefine'; eval "sub name { ... }"; } Subroutine CreateOptions redefined at C:/Perl/site/lib/Tk/TopLevel.pm +line 17 (# 2) Subroutine Populate redefined at C:/Perl/site/lib/Tk/TopLevel.pm line +22 (#2) Subroutine Icon redefined at C:/Perl/site/lib/Tk/TopLevel.pm line 29 ( +#2) Subroutine menu redefined at C:/Perl/site/lib/Tk/TopLevel.pm line 64 ( +#2) Subroutine Tk::Widget::Toplevel redefined at C:/Perl/site/lib/Tk/Widge +t.pm line 247 (#2) Tk::Error: Failed to AUTOLOAD 'Tk::Widget::TopLevel' at txt.pl line 75 [\&main::open_file] (menu invoke)
I'm not sure about any of it. Heres the code
#!/usr/bin/perl use warnings; use diagnostics; use Tk 800.000; use Tk::Frame; use Tk::TextUndo; use Tk::Text; use Tk::Scrollbar; use Tk::Menu; use Tk::Menubutton; use Tk::Adjuster; use Tk::DialogBox; my $mw=MainWindow->new; $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; $open->geometry('400x550'); my $tf = $open->Frame; my $bf = $open->Frame; $ListBox = $tf->Listbox(-height => '10', -selectmode => 'browse', -width => '40', -relief => 'sunken', -setgrid => '1'); $ListBox->delete('0.1', 'end'); opendir DIR, "."; $ListBox->insert('end', grep { -f $_ && -r $_ } readdir DIR); close DIR; $button = $bf->Button( -command => \&load, -text => 'Open'); $tf->pack; $bf->pack; $button->pack(-anchor => 'se'); } sub load{ my ($index) = $ListBox->curselection(); my $filename = $ListBox->get($index); $InputText->Load( $filename ); (my $script = $0) =~ s,.*(\/|\\),,; }

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: How can you redefine a subroutine??
by Ovid (Cardinal) on Apr 16, 2003 at 22:58 UTC

    Your error will go away if you correct the case:

    #my $open = $mw->TopLevel; my $open = $mw->Toplevel;

    However, at that point you'll discover other problems. I don't know Tk very well, so I can't comment on them.

    Cheers,
    Ovid

    New address of my CGI Course.
    Silence is Evil (feel free to copy and distribute widely - note copyright text)

Re: How can you redefine a subroutine??
by physi (Friar) on Apr 17, 2003 at 11:53 UTC
    First of all, use strict; will help you to find some variable declaration errors !

    The big problem is, that you wrote TopLevel instead of Toplevel
    This is a quick shoot for your script:

    use strict; use warnings; use Tk; my $listbox; my $mw=MainWindow->new; $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; $open->geometry('400x550'); my $tf = $open->Frame; my $bf = $open->Frame; $listbox = $tf->Listbox(-height => '10', -selectmode => 'browse', -width => '40', -relief => 'sunken', -setgrid => '1'); $listbox->delete('0.1', 'end'); opendir DIR, "."; $listbox->insert('end', grep { -f $_ && -r $_ } readdir DIR); close DIR; my $button = $bf->Button( -command => \&load, -text => 'Open'); $tf->pack; $bf->pack; $button->pack(-anchor => 'se'); } sub load{ my ($index) = $listbox->curselection(); my $filename = $listbox->get($index); $InputText->Load( $filename ); (my $script = $0) =~ s,.*(\/|\\),,; }
    As you can see, you just have to use Tk and all the other required modules need not to be used here.

    Hope this helps.

    -----------------------------------
    --the good, the bad and the physi--
    -----------------------------------