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

What is the correct way to determine if popup menu belonging to a Tk::Text widget is onscreen? "-postcommand"-option works when menu is displayed, but since there is no "unpostcommand" it doesn't help much..

Sample code:

use strict; use Tk; my $mw = MainWindow->new(); my $Text = $mw->Text()->pack; my $TextMenu = $Text->menu(); my $timer = $mw->repeat(100,\&Loop); MainLoop; sub Loop { #Try various ways to detect if PopUp-menu of Tk::Text is displ +ayed.. #Dont't work (no change when popup is displayed) my $IsMenuMapped = $TextMenu->ismapped(); #Dont't work (no change when popup is displayed) my $IsMenuViewable = $TextMenu->viewable(); #Dont't work (no change when popup is displayed) my $WhoGotFocus = $mw->focusCurrent(); #Dont't work (no change when popup is displayed) my $MenuRootY = $TextMenu->y; #Dont't work (no change when popup is displayed) my $MenuHeight = $TextMenu->height(); }

Replies are listed 'Best First'.
Re: Is Tk::Text-widget PopUp-menu onscreen?
by zentara (Cardinal) on Aug 14, 2005 at 13:20 UTC
    It was simpler than I thought above. This does it, but there still is a need to detect cascading submenus.
    #!/usr/bin/perl use warnings; use strict; use Tk; my $mw = MainWindow->new(); my $Text = $mw->Text()->pack; my $TextMenu = $Text->menu(); $TextMenu->bind("<Enter>",sub {print "1\n" }); $TextMenu->bind("<Leave>",sub {print "0\n" }); MainLoop;

    I'm not really a human, but I play one on earth. flash japh
      Hm, strange cos that was something I tried before posting here.

      Tried with different event-bindings on the Menu-widget. But Since <Motion>, <Enter> etc events didn't work (Win32/Tk800.024), I assumed that Menu-widgets didn't support bindings.. but the <Leave>-event acutally works (the event-sub is, for whatever reason, even called TWICE when the event triggers, hehe).

      Together with using the "-postcommand" option instead of the <Enter> event, I can achieve what I want.

      Thanks zentara!

Re: Is Tk::Text-widget PopUp-menu onscreen?
by zentara (Cardinal) on Aug 14, 2005 at 11:54 UTC
    Hi, interesting question. What I did was run the following code, for the first 5 seconds with no menu, then a menu for the second 5 seconds. Then compare the output. It seems that when the menu is visible, Dumper has an Entry for '_XEvent_' , which contains some binary and font information.

    I'm not sure what is the best way to detect what '_XEVENT_' is. If you figure it out, please post the code.

    #!/usr/bin/perl use warnings; use strict; use Tk; use Data::Dumper; my $mw = MainWindow->new(); my $Text = $mw->Text()->pack; my $TextMenu = $Text->menu(); my $timer = $mw->repeat(5000,\&Loop); MainLoop; sub Loop { #print Dumper[(\$TextMenu)],"\n\n"; #print join ' ',keys %$TextMenu,"\n"; #print $TextMenu{'_XEvent_'},"\n"; my %hash = %$TextMenu; print join ' ',keys %hash,"\n"; print $hash{'_XEvent_'},"\n"; }

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