Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

perl/tk changing button commands

by Anonymous Monk
on Apr 22, 2003 at 17:12 UTC ( #252319=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

i have a perl/tk program witch is just a text widget, and a button, and i want the text to change everytime i click the button. so i need to be able to change the command of the button each time it is clicked, does anybody know how to do this? here is the code:
#!/usr/bin/perl use strict; use warnings; use Tk; sub one; my $main = MainWindow->new; my $label1 = $main->Text ( '-width'=>'50' )->pack; my $button = $main->Button ( -text=>'next', -command=>sub{one} )->pack; MainLoop; sub one { my $talk2="number1"; my $talk="this text will be changing after every click of the butt +on."; $label1 -> insert('1.0',"$talk"); my $button = $main->Button(-text=>'next',-command=>sub{two})->pack +; } sub two { # change the text box again }

Comment on perl/tk changing button commands
Download Code
Replies are listed 'Best First'.
Re: perl/tk changing button commands
by Improv (Pilgrim) on Apr 22, 2003 at 17:30 UTC
    Some comments
    • I doubt you really mean to be adding all those new buttons everytime you click 'next', right? You might want to read up about how the Tk event loop works.
    • You had the syntax for -command incorrect. You either pass it an anonymous subroutine (as in the $button2 example below) or a code reference, with \&
    See below for corrected code.
Re: perl/tk changing button commands
by hiseldl (Priest) on Apr 22, 2003 at 21:38 UTC

    If you want to change the action of the button every time it is pressed, then you need to leave out the -command switch and bind the 'ButtonPress' action later. This will allow you to re-bind the action once you have pressed the button. Here's an example:

    #!/usr/bin/perl use strict; use warnings; use Tk; my $main = MainWindow->new; my $text = $main->Scrolled('Text', -width=>'50' )->pack; my $button = $main->Button(-text=>'next', )->pack; $button->bind('<ButtonPress>', \&one); MainLoop; sub one { my $talk="One! Click for two.\n"; $text -> insert('1.0',"$talk"); $button->bind('<ButtonPress>', \&two); } sub two { my $talk="One! Click for three.\n"; $text -> insert('1.0',"$talk"); $button->bind('<ButtonPress>', \&three); } sub three { my $talk="One! Click for one.\n"; $text -> delete('1.0', 'end'); $text -> insert('1.0',"$talk"); $button->bind('<ButtonPress>', \&one); }

    Cheers!

    --
    hiseldl
    What time is it? It's Camel Time!

Re: perl/tk changing button commands
by kelan (Deacon) on Apr 23, 2003 at 13:56 UTC

    How about just having an array that holds whatever pieces of text you want and looping through it. (Untested)

    #!/usr/bin/perl use strict; use warnings; use Tk; my $main = MainWindow->new; my $label1 = $main->Text( -width => 50 )->pack; my $count = 0; my @texts = ( "This is the first piece.\n", "This is the second.\n", "This is the third.\n", "etc...\n", ); my $button = $main->Button( -text => "Next", -command => sub { $label1->insert('1.0', $texts[$count++]); } )->pack; MainLoop;

    kelan


    Perl6 Grammar Student

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2015-07-30 08:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls