thegoaltender has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am have some code that prints information in a GUI based on a choice made by the user. That part works fine. What I am having a problem with is when this choice is changed the text needs to be replaced. If I just overwrite the Label and the new text is shorter than the old, some of the old text still remains. Any help on clearing this text before writing the new text?
sub replace_choice { $level[$levelselect]->Label( -text=>"$logicchoice" )->grid( -row=>1, -column=>0, -columnspan=>5, -sticky=>"nw" ); }

Replies are listed 'Best First'.
Re: Clearing text in Tk
by JediWizard (Deacon) on Sep 23, 2004 at 14:59 UTC

    I'd need to see the rest of your code to be sure, but it appears to me that you are creating a new "Label" and putting it in place over the original "Label". Perhaps you should try using -textvariable=>\$loginchoice rather than -text=>"$loginchoice". The -textvariable option for a Tk::Label widget will automatically update the label with any value you place in $loginchoice.

    May the Force be with you
      Thanks! With some modification to the code I made that work. I had ruled that out earlier because the variable could change without me wanting the text to change in the GUI. I just started working on this code again after about a month, so I am still trying to figure out what I started. Thanks again!
Re: Clearing text in Tk
by zentara (Cardinal) on Sep 23, 2004 at 16:24 UTC
    If you want to manually do the deleting in the label, you can do it with something like:
    $label->configure(-text=>''); $label->configure(-text=> $newtext);
    Or if you need to avoid the size of the label from shifting around or flickering, you could do something like:
    my $length = length $oldtext; my $text = ' ' x $length; $label->configure(-text=> $text); $label->configure(-text=> $newtext);

    I'm not really a human, but I play one on earth. flash japh