Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
So this is my rewrite of the above using a hash to track each entry which I may want to delete...
for (keys %runtime) { $widgetname = $_; $widgetrow{$widgetname} = $workframe -> Frame(); $wrLabel{$widgetname} = $widgetrow{$widgetname} -> Label(-text=>$w +idgetname) -> grid(-row=>1, -column=>1); $wrEntry{$widgetname} = $widgetrow{$widgetname} -> Entry(-width=>1 +00, -textvariable=>\$runtime{$widgetname}) -> grid(-row=>1, -column=> +2); $wrButton{$widgetname} = $widgetrow{$widgetname} -> Button(-text=> +"Delete \"$widgetname\" entry", -command=>sub { my $deltgt = $widgetname; print "Deleting \"$deltgt->$runtime{$deltgt}\"\n"; delete $runtime{$deltgt}; print "Hash entry deleted\n"; delete($wrLabel{$deltgt})->destroy(); print "Widget label deleted and destroyed\n"; delete($wrEntry{$deltgt})->destroy(); print "Widget entry deleted and destroyed\n"; delete($wrButton{$deltgt})->destroy(); print "Widget button deleted and destroyed\n"; $widgetrow{$deltgt} -> destroy(); print "Widget row destroyed\n"; } ) -> grid(-row=>1, -column=>3); $widgetrow{$widgetname} -> grid(-row=>$row, -column=>1, -columnspa +n=>3); $rowsin = $row; $row++; }
This produces a Tk frame for each variable and puts three objects in that frame: 1) the variable name, as a Tk Label, which is the key to the %runtime hash, 2) the value from the %runtime hash as a Tk Entry field and 3) a Tk Button with text saying "Delete "Variable" Entry".

After that frame is built, it's inserted in $workframe on the next available line.

This all works nicely and the buttons all have their correct variable names written on them.

However, when I press the button, ANY button, the value of $widgetname is the last key accessed from the %runtime hash and that's the entry which is deleted from the hash and the subframe which is deleted from the workframe. A subsequent attempt to use any button generates an Tk error stating that I cannot delete an undefined object.

(I put the "my $deltgt=$widgetname" line in hoping that I might sufficiently localise the variable at "build" time that I could retain it ready for execution time - didn't work, not surprised.)

Perhaps I need a mechanism to read the text from the button which invoked the subroutine. The button is generated and remains static as the loop rolls on so the target variable name can be stored therein if only I could extract that.

In reply to Re^5: Clearing anonymous Tk widgets by murrayn
in thread Clearing anonymous Tk widgets by murrayn

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (3)
    As of 2018-05-20 20:05 GMT
    Find Nodes?
      Voting Booth?