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

Hi Monks,

I'd like to run another program from my script. Both programs have a Tk interface. My problem is that when child exits, the MainWindow of the main script loses focus. It is a problem for me because the main program uses a barcode reader in keyboard emulation mode. When the MainWindow loses its focus, the bindings of the keyboard to $mw don't work, of course. I don't want to tell the operators to left-click into $mw before using the barcode reader.

Is there a way to give the focus back to $mw after the child process closes? Here is my code, it doesn't give back te focus:

$mw->iconify; $mw->withdraw; unless(open(HND,"<vmapper.ini")) { &fileRdWrError('vmapper.ini','read','p1_appendMsgToLog'); exit 0; } my $line = <HND>; chomp $line; close HND; system($line.'vmapper.exe'); #other program exited, continue from here $mw->raise; $mw->deiconify; $mw->update; $mw->focus; #should give the focus back here
Thanks,

Beci

Replies are listed 'Best First'.
Re: Tk focus
by Anonymous Monk on Jan 08, 2012 at 20:14 UTC

    I'd like to run another program from my script. Both programs have a Tk interface.

    There is your problem. What should have been a Tk::Dialog you've turned into another app.

    Anyway, it works for me, and focusForce overrides the blinking taskbar behaviour on win32 (which I'm fond of, since I don't like apps to steal the focus from me)

    #!/usr/bin/perl -- use strict; use warnings; use Tk; my $mw = tkinit(); $mw->Button( -text => 'go' , -command => sub { $mw->Busy; $mw->iconify; $mw->withdraw; system $^X, '-MTk', '-e', 'tkinit;MainLoop' ; $mw->raise; $mw->deiconify; $mw->update; $mw->focus; $mw->Unbusy; $mw->focus; $mw->focusForce; # win32 return; }, )->pack; MainLoop();

      Hi,

      This helped, the main app gets the focus back when the other exits.

      Thanks a lot for you and Khen1950fx also!

      Beci
Re: Tk focus
by Khen1950fx (Canon) on Jan 08, 2012 at 17:44 UTC
    The snippet that you've provided isn't workable. That said, you want to see the man page for Tk::focus. It's a built-in for Tk, so just enter
    man focus
    It should answer your question.