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

Hello, If I run this program and perform the following sequence:

1)select the drop down menu of the first window
2)while it is dropped down try to select the drop down menu of the second window

The first menu disappears but the second menu does not drop down. I need to click the header of the second window first and then it works.

How can I make this work without having to click the header of the second window first? Seems to be a bug of some kind but perhaps someone knows of a workaround?

Thanks!

use Tk; $top = new MainWindow(-title=>"window1"); $menu = $top->Menu(); $filemenu = $menu->Menubutton(-label => "Test"); $exitButton = $filemenu->command(-label => "Test"); $top->configure(-menu => $menu); $top2 = new MainWindow(-title=>"window2"); $menu2 = $top2->Menu(); $filemenu2 = $menu2->Menubutton(-label => "Test2"); $exitButton2 = $filemenu2->command(-label => "Test2"); $top2->configure(-menu => $menu); MainLoop();

Replies are listed 'Best First'.
Re: Problem with multiple windows and drop down menus
by zentara (Cardinal) on Jan 26, 2010 at 16:40 UTC
    Its a focus problem, not really Tk's fault. Your window manager determines which window has focus, by either "clicking on a window" or a sloppy mouse focus, where the window that the mouse is in, takes focus. Look in your window manger settings for focus type. Or Google for "your_window_manager focus"

    It would be chaos on the desktop without focus.

    You might want to redesign your app to be a single window, or at least tell us what you are trying to accomplish with this.

    The problem is compounded by the menu doing a screengrab. You might want to read perldoc Tk::focus


    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku
      Thanks. If I open the drop down menu for window 1 (ie. window 1 has focus) and then when you click the drop down menu for window 2, window 2 *appears* to take the focus (ie the title bar un-greys) but clearly the drop down menu is not working.

      However if the drop down menu in window 1 is not open (but window 1 has the focus) and I click the drop down menu for window 2, window 2 properly takes focus and the drop down menu opens up.

      So while I agree the problem is related to focus, I still think this is not behaving correctly. I have tried it on both a Sun workstation and a Windows box running Exceed and the behaviour is the same so I dont think its strictly related to a particular window manager.

      Thanks!
        The problem is that the Menu does a grabGlobal somewhere in it's internal code, so you can't leave open menus laying around. Unless that grab is released, you need to deal with it. Popups and Dialogs works that way too.... they do a grab and demand that you deal with it, by clicking a button or whatever releases the grab.

        To demonstrate the problem, look at the following and google for "perl tk grabGlobal". You can do mouse grabs, keyboard grabs, or global grabs. Your focus cannot shift until the grab is released.

        There may be a clever trick to override it, but I don't know it offhand.

        I would try something like $top->bind( '<Leave>',sub { $top->grabRelease } );

        #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new; # the window must be mapped for a global grab # so make a 1 pixel window in lower right corner # for invisible grabs $mw->geometry('200x200+50+50'); $mw->overrideredirect(1); $mw->bind("<Key>", [ \&process_key_press , Ev('K') ] ); $mw->bind("<KeyRelease>", [ \&process_key_release , Ev('K') ] ); $mw->Label(-text=>"Press escape to exit")->pack(); $mw->after(100,sub{$mw->grabGlobal;}); $mw->focusForce; MainLoop; sub process_key_press{ my ($caller, $key) = @_; print "Press $key\n"; if ($key eq 'Escape'){exit} } sub process_key_release{ my ($caller, $key) = @_; print "Release $key\n"; }

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