Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Tk create menu item dynamically

by IB2017 (Pilgrim)
on Mar 13, 2020 at 15:24 UTC ( [id://11114227]=perlquestion: print w/replies, xml ) Need Help??

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

How can I create items of a Tk pop-up menu conditionally, for example with a switch 0/1 for the first command? (Possibly still using the same way to construct the -menuitems array (since I ve got increadibly many of such menus). This is how I construct my menu:

$menu = $mw->Menu(-tearoff=>0, -menuitems=>[ [command=>$MESS_0= -command=>[\&DialogMove, $obj,]], '', [command=>$MESS_1, -command=>[sub {MoveColumn('right')}, $obj,]], [command=>$MESS_2, -command=>[sub {MoveColumn('left')}, $obj,]], '', [command=>$MESS_3, -command=>[sub {HideColumn()}, $obj,]], [command=>$MESS_4, -command=>[sub { if ($ContentColumn11GLOBAL eq 11){$ContentColumn11GLOBAL=0} else {$ContentColumn11GLOBAL=11} SetViewFromSearchFrame11($ContentColumn11GLOBAL); }, $obj,]], [command=>$MESS_5, -command=>[sub { if ($ContentColumn12GLOBAL eq 12){$ContentColumn12GLOBAL=0} else {$ContentColumn12GLOBAL=12} SetViewFromSearchFrame12($ContentColumn12GLOBAL); }, $obj,]], ]); $menu->configure(-postcommand => sub { redefine_labels(); $menu->entryconfigure(2, -label => $MESS_1); $menu->entryconfigure(3, -label => $MESS_2); $menu->entryconfigure(5, -label => $MESS_3); $menu->entryconfigure(6, -label => $MESS_4); $menu->entryconfigure(7, -label => $MESS_5); });

Replies are listed 'Best First'.
Re: Tk create menu item dynamically
by kcott (Archbishop) on Mar 14, 2020 at 03:54 UTC

    G'day IB2017,

    Referring to the title of your post, the following code dynamically creates menu items.

    #!/usr/bin/env perl use strict; use warnings; use Tk; my $mw = MainWindow::->new(); my $mb = $mw->Menubutton(-text => 'Popup'); my $menu = $mb->menu(); $mb->configure(-menu => $menu); $mb->pack(); $mw->Button( -text => 'Add menu item', -command => sub { $mb->command(-label => 'A', -command => sub { 1 }) }, )->pack(); MainLoop;

    This code is fully functional. If you run it and click "Popup", you'll see a menu with no items. Now click "Add menu item" then "Popup" again: you'll see the menu now has one item. You can repeat this process and see the number of menu items growing. You can tearoff the menu and watch it grow dynamically every time you click "Add menu item".

    As already pointed out, we can't run the code you posted. It is incomplete and it is not possible to tell you how modify it to do what you ask. Here's a few suggestions:

    • $MESS_0 to $MESS_5 does not look good. Consider an array (@MESS) and code $MESS[0] to $MESS[5].
    • In a similar vein to the $MESS_n variables, subroutine calls of the form SetViewFromSearchFrame11($arg) and SetViewFromSearchFrame12($arg) don't look good either. The names suggest they have shared functionality: consider abstracting that functionality such that calls have the form SetViewFromSearchFrame($n, $arg).
    • Look at perlop. Learn the difference between string and numerical relational and equality operators.
    • Run widget and use the "See Code" buttons. There are sections for all different types of widgets; that includes a "Menus" section.
    • See the Tk page. That has links to documentation about menus (and all other widgets).

    — Ken

Re: Tk create menu item dynamically
by Anonymous Monk on Mar 14, 2020 at 00:54 UTC

    This is how I construct my menu:

    Hi,

    Well, that is not runnable -- you should make that runnable and post an update

    How can I create items of a Tk pop-up menu conditionally, for example with a switch 0/1 for the first command? (Possibly still using the same way to construct the -menuitems array (since I ve got increadibly many of such menus).

    The best advice I have for you at the moment is make your sample runnable as a step 1

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11114227]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-23 12:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found