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

Dear monks, I have a problem with perl Tk application. I'm using a getOpenFile widget wich works fine. But there is an issue if I select the file using a double-click. The file picker dialog opens in front of the main window, then I select a file by double click, the file picker dialog closes and returns a selected file, but now comes the problem: main window recieves one additional click. Sometimes this click hits a button so an action may start unintentionally. Is somebody there who experienced this problem already? Thank you. I'm using Windows 7 64-bit with StrawberryPerl 5.12...

here is a code which reproduce a problem easily (cause of the huge button) it's easy to place the file picker window over this button and select a file using a double click. A message box opens after the huge button has been clicked to inform you.

#!/usr/local/bin/perl -w use strict; use warnings; use Tk; use utf8; use v5.12; my $mw = Tk::MainWindow->new( -title => 'main window', ); #Exit when the escape key is pressed $mw -> bind('<Key-Escape>', sub { exit }); my $frame = $mw->Frame(-borderwidth => 3)->pack( -fill => 'both',); my $btn1 = $frame->Button( -text => 'button', -command => sub { buttonClicked() }, -width => 120, -height => 40, )->pack(-fill => 'both',); $frame->Button( -text => 'file selector', -command => sub { select_file() }, )->pack(-fill => 'both',); $frame->Button( -text => 'exit', -command => sub { exit }, )->pack(-fill => 'both',); MainLoop; sub select_file { #...; my $file = $mw->getOpenFile( -title => 'select file', ); $file = File::Spec->canonpath( $file ); return $file; } sub buttonClicked { $mw->messageBox(-message=>"you clicked a button!"); }
Update: it seems to be allready known problem http://groups.google.com/group/comp.lang.perl.tk/browse_thread/thread/d18d09ac8cd1fff6/4262df12756a36bf?#4262df12756a36bf. Wonder if there is still no bug report about it.

Replies are listed 'Best First'.
Re: Tk getOpenFile doubleClick problem
by zentara (Cardinal) on Sep 13, 2011 at 15:30 UTC
    Hi, I don't notice the problem on linux, but a better hack than a 1 second delay timer, maybe be to recursively busy the mainwindow underneath, until the file is returned. It works well here. Ooops, update, I just noticed it only works the first time getOpenFile is called, after that the file dialog is busied too. There probably is a trick to fix that glitch, but it eludes as of yet. I still think busy unbusy is the way to go.

    Update 2 Yeah , removing the recurse=>1 from the busy seems to do it.

    sub select_file { #...; #$mw->Busy(-recurse => 1); #don't recurse here $mw->Busy(); my $file = $mw->getOpenFile( -title => 'select file', ); $file = File::Spec->canonpath( $file ); $mw->Unbusy(-recurse => 1); return $file; }

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

      Thank you zentara this helps!

      I tested the initial code and it works for me. Removing -recurse=>1 from ->Busy brings the problem back. Removing -recurse=>1 from ->Unbusy call is safe.
Re: Tk getOpenFile doubleClick problem
by SuicideJunkie (Vicar) on Sep 13, 2011 at 14:58 UTC

    Can you add a transparent "glass shield" widget that covers your whole window and eats the click?

    That would prevent you from accidentally hitting Big Red Buttons just like in the movies. :) I've only done it as an external script floating the shield over a separate game window, but it did work quite nicely.

Re: Tk getOpenFile doubleClick problem
by wwe (Friar) on Sep 13, 2011 at 14:35 UTC
    There is a workaround on described on http://www.rhinocerus.net/forum/lang-perl-tk/187668-checkbutton-problem.html which I also implemented in the following code. This workaround disables a click functionality for a button before calling getOpenFile and restores it after little delay. Is there a wise monk who knows more elegant solution?
    #!/usr/local/bin/perl -w use strict; use warnings; use Tk; use utf8; use v5.12; my $mw = Tk::MainWindow->new( -title => 'main window', ); #Exit when the escape key is pressed $mw -> bind('<Key-Escape>', sub { exit }); my $frame = $mw->Frame(-borderwidth => 3)->pack( -fill => 'both',); my $btn1 = $frame->Button( -text => 'button', -command => sub { buttonClicked() }, -width => 120, -height => 40, )->pack(-fill => 'both',); $frame->Button( -text => 'file selector', -command => sub { select_file() }, )->pack(-fill => 'both',); $frame->Button( -text => 'exit', -command => sub { exit }, )->pack(-fill => 'both',); MainLoop; sub select_file { breakCheckbuttonBindings(); my $file = $mw->getOpenFile( -title => 'select file', ); $file = File::Spec->canonpath( $file ); $mw->after(25,\&restoreCheckbuttonBindings); return $file; } sub buttonClicked { $mw->messageBox(-message=>"you clicked a button!"); } sub breakCheckbuttonBindings { my @tags = $btn1->bindtags; $btn1->bindtags([@tags[1,0,2,3]]); $btn1->bind('<ButtonPress>'=>sub{Tk->break}); } sub restoreCheckbuttonBindings { my @tags = $btn1->bindtags; $btn1->bindtags([@tags[1,0,2,3]]); $btn1->bind('<ButtonPress>'=>''); }