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";
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.