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

To all,

I'm currently developing a TK GUI which is dynamically created based upon specifications within a number of XML files. Part of the GUI builds up a list consisting of a picture, a label and a button. The call back (command) for each of the buttons is the same however I need to be able to work out which exact button was pressed in order to alter the correct picture.

The code below will add a number of button widgets onto a frame. Note that each button has the same call back 'print_button_label'. What I would like the code to do is print out the label associated with the button.

Also "->pack( -side => 'left', -anchor => 'w' )" doesn't seem to work within a 'Scrolled' widget?

I've done similar things in VB before by working out the currently selected widget and then reading a specific attribute of the widget object.

Any ideas?

Thanks in advance for any help provided,
Butch.
#!perl -w use strict; use warnings; use Cwd; use Tk 804; require Tk::Pane; # Buttons to be loaded onto the GUI my @buttons; $buttons[0] = "Button1"; $buttons[1] = "Button2"; $buttons[2] = "Button3"; # Create the main GUI my $MW = MainWindow->new(); $MW->title("Generic GUI creation list demo"); # Create a frame to hold all of the buttons my $master_frame = $MW->Frame( -borderwidth => 2, -relief => 'solid' )->pack( -side => 'top', -expand => 0, -anchor => 'w' ); # Create a scrolled pane within the frame my $master_pane = $master_frame->Scrolled( 'Pane', -height => 100, -width => 1000, -scrollbars => 'osoe', )->pack( -side => 'top', -anchor => 'w' ); # Add the list of pictures to the frame foreach my $button ( @buttons ) { # Create a frame for the widgets my $frame = $master_pane->Frame->pack( -side => 'top', -expand => 1, -fill => 'both', -anchor => 'w', -padx => 2, - pady => 4 ); # Add a label widget $frame->Label( -anchor => 'nw', -justify => 'right', -relief => 'groove', -text => $button, -width => 20, -height => 1 )->pack( -side => 'left', -anchor => 'w' ); # Add a button widget $frame->Button( -anchor => 'nw', -text => "change " . $button, -command => 'main::print_button_label' )->pack( -side => 'left', -anchor => 'w', -padx => 2 ); } # Run the GUI MainLoop; sub print_button_label # This operation is responsible for determining which # button was pressed and then printing the label # assocaited with the button { # Print out the label assocated with this button # # I can't work out how to do this!! # print "????\n"; }

Replies are listed 'Best First'.
Re: Currently selected TK widget.
by zentara (Cardinal) on Apr 11, 2006 at 11:39 UTC
    It's very simple, once you've seen the trick. You will see this type of callback in all sorts of Tk code.
    foreach my $button ( @buttons ) { ......... ........ # Add a button widget $frame->Button( -anchor => 'nw', -text => "change " . $button, -command => [\&print_button_label, $button], )->pack( -side => 'left', -anchor => 'w', -padx => 2 ); } ############################################### sub print_button_label{ my $button = shift; print "button\n"; }
    There are a few variations on the callback syntax. You could also do
    -command => sub { print_button_label( $button ) },

    You could also use the 'current' tag trick, where you bind a <1> press to a sub which gets the currently active widget, then use cget on the widget, to find it's name or other property; but you will usually see it as I shown above.


    I'm not really a human, but I play one on earth. flash japh
      Brilliant!

      That's exactly what I need.
      Thanks for the speedy reply.

      Butch.
Re: Currently selected TK widget.
by wulvrine (Friar) on Apr 11, 2006 at 11:24 UTC
    Have you tried the focusCurrent command, for example:
    my $who = $widget->focusCurrent;
    Then cycling through each of your buttons and checking them vs $who to see which one had focus when pressed?