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 http://www.tkdocs.com/tutorial/morewidgets.html. 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 (Archbishop) on Nov 10, 2017 at 10:18 UTC | |
Re: create listbox in tkx
by beech (Parson) on Nov 10, 2017 at 02:13 UTC | |
Re: create listbox in tkx
by Laurent_R (Canon) on Nov 10, 2017 at 07:32 UTC |