Here is one solution. Let the user pass in:
  1. A hash uses button text as key, and coderef as value
  2. An array defines the sequence of user buttons (help the hash to maintain order)
Also we use an array to store the button handlers.

#!/usr/bin/perl use Tk; use strict; use constant BUTTON_WIDTH => 20; my $val; my %user_buttons = ("button1" => \&user_button1_func, "button2" => \&user_button2_func, "button3" => \&user_button3_func); my @user_button_sequence = ("button1", "button2", "button3"); my @user_buttons; my $mw = new MainWindow; my $entry = $mw->Entry(textvariable => \$val) ->pack(fill => "x"); my $default_button1 = $mw->Button(text => "Default Button 1", width => BUTTON_WIDTH, command => sub {$val = "default butt +on 1 clicked"}) ->pack(); my $default_button2 = $mw->Button(text => "Default Button 2", width => BUTTON_WIDTH, command => sub {$val = "default butt +on 2 clicked"}) ->pack(); foreach my $button_text (@user_button_sequence) { push @user_buttons, $mw->Button(text => $button_text, width => BUTTON_WIDTH, command => $user_buttons{$button_text}) ->pack(); } MainLoop; sub user_button1_func { $val = "user button 1 clicked"; } sub user_button2_func { $val = "user button 2 clicked"; } sub user_button3_func { $val = "user button 3 clicked"; }

In reply to Re: Dynamic Tk buttons from init file by pg
in thread Dynamic Tk buttons from init file by TunesMan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.