I have the following snippet of code to a larger Tk GUI application I'm working on enhancing. This particular code here generates a widget, accepts a button click and from there gives you a browse entry to pick an option from. Problems arise because when you hit the button many times it generates as many browse entry widgets which I don't want.
#!/usr/local/bin/perl use strict; use warnings; use Tk; require Tk::BrowseEntry; ######################### #Hitting the button casuses many instances of listBrowse widget ######################## my $main= MainWindow->new(); my $listButton=$main->Button( -text=> 'list', -height=> 2, -width=> 10, -relief=> 'ridge', -command=> \&listBrowse, )->pack(); my $choice="number"; #the default list item. sub listBrowse{ my $listTag=$main->BrowseEntry( -variable=>\$choice, -browsecmd=>\&Verify )->pack(); $listTag->insert("end","Number"); $listTag->insert("end","Bullet"); } MainLoop;

The button method of Tk doesn't have an option like "-variable" that can make me control this behavior in a better way. Though, I suspect an option like "-default" can be handy, I couldn't get hold of documentation for it when I checked around. So I've tried to manually introduce a flag which changes value when the button is pushed, I couldn't figure out how to implement this flag into some useful logic that could allow the list button to be hit only once. Ergo my reversion to the revered panel of Monks for guidance on how to be able to hit a button and activate the consequent action only once and not more than once.

UPDATE: I've used the tip provided by lamprecht and could extend on it to re-enable the disabled button and refresh the entire widget anew. SuicideJunkie, the second paragraph from your post was an inspiration. I was trying to use the flag the same way and introducing the same extra scope before I posted this question and it couldn't work in this block though another part of the application used a flag effectively. Thank ya'all.

#the steps in the refresh were to enable a disabled widget and destroy + a created #widget, these two behaviors can be kept under a button to + work simultaneously $listButton->configure(-state=>'normal'); $listTag->destroy if Tk::Exists($listTag);


Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.

In reply to better control of Tk::Button by biohisham

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.