Click to See Complete Forum and Search --> : Perl/Tk


drstrangelove
01-18-2001, 11:17 AM
can someone help.
I don't want to be told how to do it, just a few suggestions....
I have started writing a program (that does the same thing as a previous one i wrote that is posted on this board) with Perl/Tk.
I think I am doing it right, because when I click the "no" button I created, it does what it's supposed to.
I created a "yes" button to open a new window, with more choices, but nothing happens when I click it.
Can someone peruse my code and tell me what is wrong and just give some suggestions as to how I could fix it?
Thanks,
Dr. Strangelove


use Tk;
#Windows
$MW = new MainWindow;
$MW->title("MSI");
#Text Labels
$MW->Label("-textvariable"=>\$message,
"-relief"=>'groove',
"-borderwidth"=>2)->pack("-fill"=>'x',
"-side"=>'bottom');
$MW->Label("-text"=>'This program will allow you to add new antennas to your current antenna database. Would you like to continue?',
"-relief"=>'groove',
"-borderwidth"=>2,
"-wraplength"=>100)->pack("-fill"=>'x',
"-side"=>'top');
#Buttons
$B1 = $MW->Button("-text"=>"Yes"
"-command"=>sub {ru_sure;})->pack(-side=>'left',
-fill=>'x',
-expand=>1);
&bind_message($B1, "Press to continue.");

$B2 = $MW->Button("-text"=>"No",
"-command"=>sub
{ exit;
})->pack(-side=>'left',
-fill=>'x',
-expand=>1);
&bind_message($B2, "Press to exit.");
#Subroutines
sub bind_message{
my ($widget, $msg) = @_;
$widget->bind('<Enter>',[sub{
$message = "$_[1]";
},
$msg]);
$widget->bind('<Leave>', sub{
$message = "";
});
}
sub ru_sure{
my $backup_win = $MW->new->title("ALERT!");
$backup_win->Label("-text"=>'Are you sure you have backed up your current antenna database and your new antenna file?',
"-wraplength"=>100,
"-relief"=>'groove',
"-borderwidth"=>2)->pack(-side=>'top',
-fill=>'x');
my $B3 = $backup_win->Button("-text"=>"Yes",
"-command"=>sub {exit;
})->pack(-side=>'left');
&bind_message($B3, "I have backup up, and I am ready to continue.");
my $B4 = $backup_win->Button("-text"=>"No",
"-command"=>sub {exit;
})-pack(-side=>'right');
&bind_message($B4, "I need to backup before we continue.");
}

MainLoop;


[This message has been edited by drstrangelove (edited 18 January 2001).]

YaRness
01-18-2001, 11:39 AM
i don't know ANYTHING about that TK module.

that being said, i noticed in one of your "no" buttons you have

"-command"=>sub
{ exit;
}

whereas there doesn't seem to be any similar command thingy in your "yes" button:


$B1 = $MW->Button("-text"=>"Yes")->pack(-side=>'left',
-fill=>'x',
-expand=>1);
&bind_message($B1, "Press to continue.");


hope that helps

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/

drstrangelove
01-18-2001, 11:43 AM
I noticed the same thing.
so I added

$B1 = $MW->Button("-text"=>"Yes"
"-command"=>sub {ru_sure;})->pack(-side=>'left',
-fill=>'x',
-expand=>1);
&bind_message($B1, "Press to continue.");

I added the sub {ru_sure;}
but now I get an error
'String found where operator expected at U:\Perl Files\antenna_project\tk\antenna.pl line 17, near ""-command""'
I am new and trying hard to learn this, but I am confused...
Thanks in advance.

------------------
Never go up against a Sicilian when death is on the line!!
Hahahahahahaha***

corrumpu
01-18-2001, 11:57 AM
alright. i don't like getting advice like this either.. but i think it might help you.
www.perltk.org (http://www.perltk.org) <--- very few examples but...
and there is a perltk newsgroup... lots of people with LOADS of good knowledge..
i've written some perl/tk but i too have had some problems with binding or using subs on buttons. another great thing to do is search freshmeat for perl/tk .. quite a few progs are written with the tk module and are a great source for just the kind of question you asked.

good luck

::chris::

hope this didn't look like a "RTFM"

drstrangelove
01-18-2001, 04:09 PM
yeah there were a lot of helpful docs to print out.
However...
here's my code:

use Tk;
#Windows
$MW = new MainWindow;
$MW->title("MSI");
#Text Labels
$MW->Label("-textvariable"=>\$message,
"-relief"=>'groove',
"-borderwidth"=>2)->pack("-fill"=>'x',
"-side"=>'bottom');
$MW->Label("-text"=>'This program will allow you to add new antennas to your current antenna database. Would you like to continue?',
"-relief"=>'groove',
"-borderwidth"=>2,
"-wraplength"=>100)->pack("-fill"=>'x',
"-side"=>'top',
"-expand"=>1);
#Buttons
$B1 = $MW->Button("-text"=>"Yes",
"-command"=>\&ru_sure)->pack(-side=>'left',
-fill=>'x',
-expand=>1);
&bind_message($B1, "Press to continue.");

$B2 = $MW->Button("-text"=>"No",
"-command"=>sub
{ exit;
})->pack(-side=>'left',
-fill=>'x',
-expand=>1);
&bind_message($B2, "Press to exit.");
#Main Loop
MainLoop;

#Subroutines
sub bind_message{
my ($widget, $msg) = @_;
$widget->bind('<Enter>',[sub{
$message = "$_[1]";
},
$msg]);
$widget->bind('<Leave>', sub{
$message = "";
});
}
sub ru_sure{
my $backup_win = $MW->new;
$backup_win->title("ALERT!");
$backup_win->Label("-text"=>'Are you sure you have backed up your current antenna database and your new antenna file?',
"-wraplength"=>100,
"-relief"=>'groove',
"-borderwidth"=>2)->pack(-side=>'top',
-fill=>'x');
$B3 = $backup_win->Button("-text"=>"Yes",
"-command"=>sub {exit;
})->pack(-side=>'left');
&bind_message($B3, "I have backup up, and I am ready to continue.");
$B4 = $backup_win->Button("-text"=>"No",
"-command"=>sub {exit;
})-pack(-side=>'right');
&bind_message($B4, "I need to backup before we continue.");
}




Here's my error:

Tk::Error: Can't locate object method "apply_command_line" via package "MainWindow=HASH(0x241f3d0)" at D:/Perl/site/lib/Tk/MainWindow.pm line 57.
[\&main::ru_sure]
Tk callback for .button
Tk::__ANON__[D:/Perl/site/lib/Tk.pm:217] at D:/Perl/site/lib/Tk.pm line 217
Tk::Button::butUp at D:/Perl/site/lib/Tk/Button.pm line 113
(command bound to event)

Any suggestions would be greatly appreciated...

YaRness
01-18-2001, 04:46 PM
i'd find people familiar with those tk modules.

my only guesses would be maybe "\&ru_sure" needs to be "&ru_sure" or just "ru_sure", depending on what that Command=> parameter is expecting. prolly wouldn't hurt to try at any rate.

good luck.

------------------
"Assembly of Japanese bicycle require great peace of mind."
Registered Linux User #188285 http://counter.li.org/