in reply to Popup Menus on Tk::Notebook tabs

Hi,

here is a short example (check Tk::bind for valid Event symbols/characters):

use warnings; use strict; use Tk; my $mw = tkinit; my $n = $mw->NoteBook()->pack; for(qw/foo bar baz/){$n->add($_, -label => $_, );} $n->bind('<3>',[\&process_bt3,Ev('x'),Ev('y')]); MainLoop; sub process_bt3{ my ($w, $x, $y) = @_; my $selected = $w->identify($x,$y); print "selected Tab: $selected\n" if( $selected ); }

Cheers, Christoph

update: link for bind doc

Replies are listed 'Best First'.
Re^2: Popup Menus on Tk::Notebook tabs
by skewray (Initiate) on Sep 22, 2009 at 00:56 UTC
    Here is the code that I came up with a few hours after posting, mostly cribbed from trolling the web. I have it in a .pm file:
    use warnings ; use strict ; use Tk ; use Tk::NoteBook ; my $popup_menu_items = [ [Button => "Close", -command => \&popup_close], [Button => "Other", -command => \&popup_other] ] ; my $tabs ; my $popup_menu ; sub sk_notebook_add { my $MW = shift ; $tabs = $MW->NoteBook()->pack( -fill=>'both', -expand=>1 ); $popup_menu = $tabs->Menu( -menuitems => $popup_menu_items, -tearo +ff => 0 ) ; $tabs->bind( "<Button-3>" => [ \&popup_spot, Ev('X'), Ev('Y') ] ) +; return $tabs ; } my $popup_tab_name ; sub popup_spot { my $x = $_[1] - $tabs->rootx ; my $y = $_[2] - $tabs->rooty ; $popup_tab_name = $tabs->identify( $x, $y ) ; $popup_menu->Popup( -popover => "cursor", -popanchor => 'nw' ) if( + $popup_tab_name ) ; } sub popup_close { $tabs->delete( $popup_tab_name ) ; } sub popup_other { print "name = $popup_tab_name\n" ; } 1 __END__
    I have only been programming Perl for a few weeks, so I am not sure what correct style is, but the 'global' variables in the module bother me. I am not sure how else to hand the appropriate widget to the popup subroutines, or even if having a cute way to do so is good Perl.

    Brian