#!/usr/bin/perl use strict; use warnings 'all'; use Tk; use Tk::KMenu; # create main window my $mw = tkinit(); # create menu my $menu = new Tk::KMenu $mw; # add menu entries $menu -> add( Test => sub { print "Test\n"; }, Test2 => sub { print "Test 2\n"; }, Delete => sub { $menu -> del( 0 .. 2 ); }, Beenden => sub { $mw -> destroy; }, ); # should create a package for MenuButtons, so that the following can be written simpler # # create menubutton # my $menubutton = $mw -> Button( -text => 'Open Menu', ) -> pack; # have to split this because $menubutton has to be defined, # so we can use it in the -command callback. $menubutton -> configure( -command => sub { $menu->show( $mw->rootx + $menubutton->x, # calculate the x position $mw->rooty + $menubutton->y # calculate the y position + $menubutton->reqheight, ); }, ); MainLoop;