http://www.perlmonks.org?node_id=991375


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

Solved! The key is in the -command and passing parameters to a callback on a widget. The code above doesn't work because "$widgetname" is being evaluated at the time the button is released by which time the value has changed from what was intended. The challenge is to tell the callback associated with the button what the value was at the time the button was created and that is done by passing it as an argument to the callback thus:
-command=> [sub { my $deltgt = shift; 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"; }, $widgetname]

Replies are listed 'Best First'.
Re^7: Clearing anonymous Tk widgets
by Anonymous Monk on Sep 03, 2012 at 16:39 UTC

    at the time the button was created and that is done by passing it as an argument to the callback thus:

    Or using a proper closure like I showed twice already