Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Tk Listbox problem

by Sisk (Novice)
on Feb 19, 2009 at 03:17 UTC ( [id://744930]=perlquestion: print w/replies, xml ) Need Help??

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

Greatings monks. Here's my problem. I'm trying to write a launcher for my NES, SNES, and Genesis emulators but my code isn't working right. When I try to run it, I get 'Can't call method "delete" on an undefined value'. The line number it points at is the first line of the changeList subroutine. Anyway, here's the code. Can anyone tell me what's wrong with it?
#!/usr/bin/perl use Tk; use Switch; # Variables for emulators $snesCommand="zsnes"; $genesisCommand="dgen"; $nesCommand="nestra"; $snesDirectory="~/roms/nes/"; $genesisDirectory="~/roms/genesis/Roms/"; $snesDirectory="~/roms/snes/Roms/"; $mw=MainWindow->new(-title=>"Launcher"); #Main window elements $systemPick=$mw->Optionmenu(-command=>\&changeList, -variable=>\$system); $systemPick->addOptions("SNES","NES","Genesis"); $opt=$mw->Scrolled("Listbox",-selectmode=>'single', -scrollbars=>"se"); $go=$mw->Button(-text=>"Launch Game", -command=>\&gogo); # Geometry $systemPick->pack(); $opt->pack(-expand=>1,-fill=>"both"); $go->pack(); sub changeList { $opt->delete(0,'end'); switch ($system) { case "SNES" { $command=$snesCommand; $directory=$snesDirectory; @dir=glob($snesDirecotry); foreach my $file(@dir) { $file=~s/$directory//; $opt->insert('end',$file); } } case "NES" { $command=$nesCommand; $directory=$nesDirectory; @dir=glob($nesDirecotry); foreach my $file(@dir) { $file=~s/$directory//; $opt->insert('end',$file); } } case "Genesis" { $command=$genesisCommand; $directory=$genesisDirectory; @dir=glob($genesisDirecotry); foreach my $file(@dir) { $file=~s/$directory//; $opt->insert('end',$file); } } } } sub gogo { my $selected=$opt->get($opt->curselection()); system("$command $directory$selected"); } &changeList; MainLoop;

Replies are listed 'Best First'.
Re: Tk Listbox problem
by Erez (Priest) on Feb 19, 2009 at 08:17 UTC

    You're calling &changeList before the ListBox is actually rendered (with MainLoop). There is nothing *to* delete when it's running. Same error will be thrown on the insert method.

    Also, use eq instead of == when you're comparing strings (e.g. if ($system eq "SNES"))

    "A core tenant of the greater Perl philosophy is to trust that the developer knows enough to solve the problem" - Jay Shirley, A case for Catalyst.

Re: Tk Listbox problem
by Anonymous Monk on Feb 19, 2009 at 04:06 UTC
    1) you're using Switch module (the author himself says you shouldn't use it).
    2) you're not using strict/warnings
    3) you're not using <code></code> tags
      Ok, I rewrote it using strict and warnings and replaced the switch with if/elsif statements. I have $opt declared as a global, but it's throwing the exact same error it was before For some reason I had a brain fart and forgot to use the <code> tags. Sorry about that. It's fixed now.
      #!/usr/bin/perl use Tk; use warnings; use strict; # Variables for emulators use vars qw($snesCommand $genesisCommand $nesCommand $snesDirectory $n +esDirectory $genesisDirectory $opt $system $directory $command); $snesCommand="zsnes"; $genesisCommand="dgen"; $nesCommand="nestra"; $snesDirectory="~/roms/nes/"; $genesisDirectory="~/roms/genesis/Roms/"; $snesDirectory="~/roms/snes/Roms/"; my $mw=MainWindow->new(-title=>"Launcher"); my $systemPick=$mw->Optionmenu(-command=>\&changeList, -variable=>\$system); $systemPick->addOptions("SNES","NES","Genesis"); $opt=$mw->Scrolled("Listbox",-selectmode=>'single', -scrollbars=>"se"); my $go=$mw->Button(-text=>"Go for it", -command=>\&gogo); # Geometry $systemPick->pack(); $opt->pack(-expand=>1,-fill=>"both"); $go->pack(); sub changeList { $opt->delete(0,'end'); if ($system=="SNES") { $command=$snesCommand; $directory=$snesDirectory; my @dir=glob($snesDirectory); foreach my $file(@dir) { $file=~s/$directory//; $opt->insert('end',$file); } } elsif ($system=="NES") { $command=$nesCommand; $directory=$nesDirectory; my @dir=glob($nesDirectory); foreach my $file(@dir) { $file=~s/$directory//; $opt->insert('end',$file); } } elsif ($system=="Genesis") { $command=$genesisCommand; $directory=$genesisDirectory; my @dir=glob($genesisDirectory); foreach my $file(@dir) { $file=~s/$directory//; $opt->insert('end',$file); } } } sub gogo { my $selected=$opt->get($opt->curselection()); system("$command $directory$selected"); } &changeList; MainLoop;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://744930]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2024-04-25 06:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found