Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Really disable a button in Tk

by Anonymous Monk
on Dec 12, 2022 at 07:54 UTC ( [id://11148771]=perlquestion: print w/replies, xml ) Need Help??

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

When a button is in the disabled state it does not respond -as advertised, but once the button is enabled again it responds to all presses that it received when it was in the disabled state. I can't figure out how to eliminate this behavior. I do not want the button to respond to anything that happened when it was disabled -ever. Can anyone help me with this? Here's a program to demonstrates the problem, it disables all buttons for 4 seconds whenever one is pressed. Thanks

use Tk; my $mw = MainWindow->new; my %btns; for (qw(alpha beta gamma)){ my $name = $_; $btns{$_} = $mw->Button( -text => $_, -command => sub{foo($name)}, )->pack; } MainLoop; sub foo{ my $name = shift; for (keys %btns){ $btns{$_}->configure(-state => 'disabled'); } my %dispatch =( alpha => \&alpha, beta => \&beta, gamma => \&gamma, ); $dispatch{$name}->($name); for (keys %btns){ $btns{$_}->configure(-state => 'normal'); } } sub alpha{ print "alpha\n"; sleep 4; } sub beta{ print "beta\n"; sleep 4; } sub gamma{ print "gamma\n"; sleep 4; }

Replies are listed 'Best First'.
Re: Really disable a button in Tk
by Corion (Patriarch) on Dec 12, 2022 at 08:01 UTC

    Don't call sleep from within your Tk application. Doing anything blocking like that will make the whole application unresponsive and not handle any events, and when you wake up from the sleep, all unhandled events will be handled again.

    If you want to sleep look at a Tk->after like in this demo.

    If you want to run a long running process that actually does some work, do that in a thread or outside of your UI program, or split it up into small chunks so that you can return to the Tk mainloop in between.

Re: Really disable a button in Tk
by tybalt89 (Monsignor) on Dec 12, 2022 at 08:12 UTC
    #!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11148771 use warnings; use Tk; my $mw = MainWindow->new; my %btns; for (qw(alpha beta gamma)){ my $name = $_; $btns{$_} = $mw->Button( -text => $_, -command => sub{foo($name)}, )->pack; } MainLoop; sub foo{ my $name = shift; for (keys %btns){ $btns{$_}->configure(-state => 'disabled'); } my %dispatch =( alpha => \&alpha, beta => \&beta, gamma => \&gamma, ); $dispatch{$name}->($name); } sub normal { for (keys %btns){ $btns{$_}->configure(-state => 'normal'); } } sub alpha{ print "alpha\n"; # sleep 4; $mw->after( 4_000 => \&normal ); } sub beta{ print "beta\n"; # sleep 4; $mw->after( 4_000 => \&normal ); } sub gamma{ print "gamma\n"; # sleep 4; $mw->after( 4_000 => \&normal ); }
Re: Really disable a button in Tk
by Anonymous Monk on Dec 12, 2022 at 12:45 UTC
Re: Really disable a button in Tk
by Anonymous Monk on Dec 12, 2022 at 17:22 UTC

    Thank you all, that's just what I was looking for. And now I also have a better understanding of the event loop.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11148771]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 05:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found