Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

create listbox in tkx

by penmkr (Novice)
on Nov 09, 2017 at 22:31 UTC ( #1203087=perlquestion: print w/replies, xml ) Need Help??
penmkr has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks: I have been attempting to learn perl/tk (tkx). This is made much more difficult because the code in the tutorials don't seem to work as printed. I have managed to get some of the easier ones working but this one is driving me nuts. The code below was taken from This line of code $lbox = $content->new_tk__Listbox(-listvariable => \$cnames, -height => 10); should create a listbox but returns an error. I have been unable to find a combination that works. Everything I try gets a 'invalid command' error. I am using activestate perl vers 5.24.1 using a windows 10 system. I would really appreciate any help you can give me to get this code to run. Thanks

use tkx; # InitiaLize our country "databases": # - the List of country codes (a subset anyway) # - a parallel list of country names, in the same order as the count +ry codes # - a hash table mapping country code to population @countrycodes = ("ar" ,"au", "be", "br", "ca", "cn", "dk", "fi", "fr", + "gr", "in", "it", "jp", "mx", "nl", "no", "es", "se", "ch"); @countrynames = ("Argentina", "Australia", "Belgium", "Brazil", "Canad +a", "China", "Denmark", "Finland", "France", "Greece", "India", "Italy", "Jap +an", "Mexico", "Netherlands", "Norway", "Spain", "Sweden", + "Switzerland"); %populations = ("ar" => 41000000, "au" => 21179211, "be" => 10584534, +"br" => 185971537, "ca" => 33148682, "cn" => 1323128240, "dk" => 5457415, + "fi" => 5302000, "fr" => 64102140, "gr" => 11147000,"in" => 11310430 +00, "it" => 59206382, "jp" => 127718000, "mx" => 106535000, "nl"=> 16402414, + "no" => 4738085, "es" => 45116894, "se" => 9174082, "ch" => 7508700); # Names of the gifts we can send %gifts = ("card" => "Greeting card", "flowers" => "Flowers", "nastygra +m" => "Nastygram"); # Create and grid the outer content frame $mw = Tkx::widget->new("."); $content = $mw->new_ttk__frame(-padding => "5 5 12 0"); $content->g_grid(-column => 0, -row => 0, -sticky => "nwes"); $mw->g_grid_columnconfigure(0, -weight => 1); $mw->g_grid_rowconfigure(0, -weight => 1); # Create the different widgets; note the variabLes that many # of them are bound to, as well as the button callback. # The Listbox is the onLy widget we'll need to refer to dirctly # Later in our program, so for convenience we'll assign it to a variab +le. # Remember that we must us a tcl formatted list for listvariable. $cnames = ''; foreach $i (@countrynames) {$cnames = $cnames . ' {' . $i . '}';}; $lbox = $content->new_tk__Listbox(-listvariable => \$cnames, -height = +> 10); $lbl = $content->new_ttk__label(-text => "Send to country's leader:"); $gl = $content->new_ttk__radiobutton(-text => $gifts{'card'}, -variabl +e => \$gift, -value => 'card'); $g2 = $content->new_ttk__radiobutton(-text => $gifts{'flowers'}, -vari +able => \$gift, -value => 'flowers'); $g3 = $content->new_ttk__radiobutton(-text => $gifts{'nastygram'}, -va +riable => \$gift, -value => 'nastygram'); $send = $content->new_ttk__button(-text => "Send Gift", -command => su +b {sendGift()}, -default => 'active'); $l1 = $content->new_ttk__label (-textvariable => \$sentmsg, -anchor = +> "center"); $l2 = $content->new_ttk__label (-textvariable => \$statusmsg, -anchor +=> "w"); # Grid all the widgets $lbox->g_grid(-column => 0, -row => 0, -rowspan => 6, -sticky => "nsew +"); $lbl->g_grid(-column => 1, -row => 0, -padx => 10, -pady => 5); $gl->g_grid(-column => 1, -row => 1, -sticky => "w", -padx => 20); $g2->g_grid(-column => 1, -row => 2, -sticky => "w", -padx => 20); $g3->g_grid(-column => 1, -row => 3, -sticky => "w", -padx => 20); $send->g_grid(-column => 2, -row => 4, -sticky => "e"); $11->g_grid(-column => 1, -row => 5, -columnspan => 2, -sticky => "n", + -pady => 5, -padx => 5); $12->g_grid(-column => 0, -row => 6, -columnspan => 2, -sticky => "we" +); $content->g_grid_columnconfigure(0, -weight => 1); $content->g_grid_rowconfigure(0, -weight => 1); # Set event bindings for when the seLection in the Listbox changes, # when the user doubLe clicks the List, and when they hit the Return k +ey $lbox->g_bind("<<ListboxSelect>>", sub {showPopulation()}); $lbox->g_bind("<Double-1>", sub {sendGift()}); $mw->g_bind("<Return>", sub {sendGift()}); # Called when the selection in the Listbox changes; figure out # which country is currentLy selected, and then Lookup its country # code, and from that, its popuLation. Update the status message # with the new population. As well, cLear the message about the # gift being sent, so it doesn't stick around after we start doing # other things. sub showPopulation { my @idx = $lbox->curselection; if ($#idx==0) { my $code = $countrycodes[$idx[0]]; my $name = $countrynames[$idx[0]]; my $popn = $populations{$code}; $statusmsg = "The population of " . $name . "(" . $code . ") is $ +popn"; } $sentmsg = ""; } # Called when the user doubLe clicks an item in the Listbox, presses # the "Send Gift" button, or presses the Return key. In case the selec +ted # item is scrolled out of view, make sure it is visible. # # Figure out which country is selected, which gift is selected with th +e # radiobuttons, "send the gift", and provide feedback that it was sent +. sub sendGift { my @idx = $lbox->curselection; if ($#idx==0) { $lbox->see($idx[0]); # LI +NE 94 my $name=$countrynames[$idx[0]]; # Gift sending Left as an exercise to the reader $sentmsg = "Sent " . $gifts{$gift} . " to leader of " . $name; } } # Colorize alternating Lines of the Listbox for ($i=0; $i<=$#countrynames; $i+=2) { $lbox->itemconfigure($i, -background => "#f0fOff"); } # Set the starting state of the interface, including the # defauLt gift to send, and cLearing the messages. Select the first # country in the List; because the <<ListboxSelect>> event is only # generated when the user makes a change, we explicitly call showPopul +ation. $gift = 'card'; $sentmsg = ""; $statusmsg = ""; $lbox->selection_set($idx[0]); +#LINE 113 showPopulation; Tkx::MainLoop();

Replies are listed 'Best First'.
Re: create listbox in tkx
by kcott (Chancellor) on Nov 10, 2017 at 10:18 UTC

    G'day penmkr,

    You asked an almost identical question just over a fortnight ago: "creating a listbox in tkx". That also confused Tk with Tkx; contained an incorrectly copied method name for creating listboxes; complained about the tutorials; and presented URLs, not as links, but as plain text.

    I spent a fair amount of time responding to all of these issues; as well as providing a large amount of additional information (including a lot of links to related documentation): "Re: creating a listbox in tkx".

    Go back and read it (again). The answers haven't changed.

    — Ken

Re: create listbox in tkx
by beech (Vicar) on Nov 10, 2017 at 02:13 UTC


    This is made much more difficult because the code in the tutorials don't seem to work as printed.

    Are you copy/pasting the code?

    The tutorial says new_tk__listbox but your code has new_tk__Listbox

    When I copy/paste from the website the code "works" for me (gui shows, buttons click)

Re: create listbox in tkx
by Laurent_R (Canon) on Nov 10, 2017 at 07:32 UTC
    I have been attempting to learn perl/tk (tkx). This is made much more difficult because the code in the tutorials don't seem to work as printed.
    Not at all an expert on this, but perhaps this has to do with the fact that Perl/Tk and Tkx are two different things, albeit somewhat similar. So if you're using one while using a tutorial or a book for the other, you'll quite plausibly run into various syntax and other kinds of problems. (Just my two cents, please disregard if my hypothesis is wrong.)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1203087]
Approved by holli
[Your Mother]: Heh. I keeping seeing related headlines but my narcissism is a perihelion right now so I'm busy. :P
[LanX]: https://www. news/series/ cambridge- analytica-files
[Your Mother]: s/a /at /;
[LanX]: just watch the video ...
[Your Mother]: Roger, will co.
[Your Mother]: The good news is, they think they are influencing people but Americans at least are irretrievably biased. All that can be accomplished is a little spike in domestic violence. :P

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (11)
As of 2018-03-19 13:23 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (240 votes). Check out past polls.