Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to really disable a button in Perl/Tk 1

Status
Not open for further replies.

raider2001

Technical User
Apr 27, 2001
488
US
I'm trying to disable a button using Perl's Tk module. Setting the state to 'disabled' doesn't really disable the button, it just delays the action until after the button is re-enabled. I want to ignore all mouse clicks that occur when the button is disabled. Here's some code to show the problem. If I click when the button is disabled (sleep), the ResetButton subroutine is called after the sleep.
Code:
use Tk;
$Main = MainWindow->new(); 
$Frame = $Main->Frame();
$Frame->pack();
$Button = $Frame->Button(-text=>'Start', -command=>\&Start);
$Button->pack();
MainLoop();

sub Start() {
    $Button->configure(-state => 'disabled');
    $Button->update;
    sleep(10); # If I click at this time - ResetButton is called later
	$Button->configure(-state, 'normal', -command, \&ResetButton, -background, 'green', -activebackground, 'green', -text, 'PASSED', -command => \&ResetButton );
    $Button->update;
} 

sub ResetButton() {
    $Button->configure(-text, 'Start', -activebackground, 'grey', -background, 'grey', -command => \&Start);
    $Button->update;
}
 
Why don't you unpack it when not in use then pack it for use - that should really do it.
 
I thought about that, but I'm using the button to show that the test is running. This is done by changing the label on the button. I guess I could put a label in the same frame instead.
 
Well here's what I ended up doing. When the start button is pushed, it is unpacked and a run button is created to show the test is running. When the test is done, the run button is unpacked and the start button is re-packed.

Thanks greadey!

Here's the code:
Code:
use Tk;
$Main = MainWindow->new(); 
$Frame = $Main->Frame();
$Frame->pack();
$Button = $Frame->Button(-text=>'Start', -command=>\&Start);
$Button->pack();
MainLoop();

sub Start() {
    $Button->packForget;
    $RunButton = $Frame->Button(-text=>'Running');
    $RunButton->pack();
    $Frame->update;
    sleep(10); 
    $RunButton->packForget;
    $Button->configure(-text, 'Done', -command => \&ResetButton);
    $Button->pack();
} 

sub ResetButton() {
    $Button->configure(-text, 'Start', -activebackground, 'grey', -background, 'grey', -command => \&Start);
    $Button->update;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top