Here's a working (albeit barebones) example to show the techniques you'll probably need. (See Notes at the end.)

#!/usr/bin/env perl use strict; use warnings; use autodie; use Tk; use Tk::Pane; my $mw = MainWindow->new(); $mw->geometry('200x250+50+50'); my $connection_file = './pm_tk_dynamic_buttons_connection.txt'; my $action_F = $mw->Frame()->pack(-side => 'bottom'); $action_F->Button( -text => 'Change Connections', -command => [\&change_connections, \$connection_file], )->pack; $action_F->Button(-text => 'Exit', -command => sub { exit })->pack; my $pane_F = $mw->Frame()->pack(-fill => 'both', -expand => 1); $pane_F->pack(-fill => 'both', -expand => 1); my $button_P = $pane_F->Scrolled('Pane', -scrollbars => 'osoe', -sticky => 'nsew' )->pack(-fill => 'both', -expand => 1); my $text_F = $mw->Frame()->pack(-fill => 'both', -expand => 1); my $pressed_T = $text_F->Scrolled('Text', -scrollbars => 'osoe', -wrap => 'none', -height => 10 )->pack(-fill => 'both', -expand => 1); my $delay = 100; $mw->repeat($delay, [\&check_connections, \$connection_file, \$button_P, \$pressed_T] ); MainLoop; { my %connect_button_for; my $last_mod; sub check_connections { my ($file_ref, $but_win_ref, $out_win_ref) = @_; if (-e $$file_ref) { my $cur_mod = -M $$file_ref; return if defined $last_mod && $cur_mod == $last_mod; $last_mod = $cur_mod; $_->destroy for values %connect_button_for; %connect_button_for = (); open my $fh, '<', $$file_ref; /handle=(\d+)/ and ++$connect_button_for{$1} while <$fh>; close $fh; for my $handle (sort keys %connect_button_for) { $connect_button_for{$handle} = $$but_win_ref->Button( -text => "Handle $handle", -command => sub { $$out_win_ref->insert( end => "Clicked 'Handle $handle' button.\n +" ); $$out_win_ref->yview('end'); }, )->pack; } } } } BEGIN { my @all_connections = ( ['>' => 1], ['>>' => 2, 3], ['>' => 2, 4], ['>>' => 5, 3], ['>'], ); my $connection_index = -1; sub change_connections { my ($file_ref) = @_; my $index = ++$connection_index % @all_connections; my @connectons = @{$all_connections[$index]}; my $mode = shift @connectons; open my $fh, $mode, $$file_ref; print $fh "... handle=$_ ...\n" for @connectons; close $fh; } } END { unlink $connection_file }

Notes

Update: I had a misplaced closing brace in check_connections() (putting the for loop outside the if (-e $$file_ref) block — it should've been inside that block). This didn't affect how the routine currently functioned but may have introduced a weird bug if subsequent changes were made. Fixed and successfully retested.

-- Ken


In reply to Re^3: run command every x seconds by kcott
in thread run command every x seconds by amboxer21

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.