kurtasbestos has asked for the wisdom of the Perl Monks concerning the following question:
I know this is a Perl website and all, and I'm working strictly in Tcl/Tk, but whenever I dig around the Internet for Tcl/Tk guidance with my current project, for some reason I almost always find the answers here. Unfortunately, it's now come to a halt over what seems like a really simple/stupid problem so I decided I'd see if someone can help. Again, sorry it's not Perl I've already found several very similar threads here, and you seem like a very helpful bunch.
Basically, I have a simple popup window with a bunch of text entry boxes. For one of those boxes, called "manufacturer", I want to have a button that summons a popup menu so the user can select from several common manufacturers instead of having to type it in every time. The text entry box is still there, however, so that they can type in a manufacturer that's not contained in the list. The list of manufacturers is contained in a .txt document so that it's super easy to add and remove manufacturers at any time.
It seems like it should be really simple... the menu pops up the way I want, shows all of the manufacturers taken from the txt file, and fills in the entry box when you select something. However, it only fills in the entry box with the last entry in the list (Victrola). I've tried all kinds of variations and weird menu-related commands to get this thing to work, but it seems like that even though the "add command" part of setting up the menu gets the labels right, they all run the same command (the menu index is set to whatever Victrola is, rather than being different for every entry).I would greatly appreciate any help that someone can offer! Here's the code:
### get the list of manufacturers from the .txt file set manufacturers {} set fid [open manufacturers.txt] while {[gets $fid line] != -1} { lappend manufacturers $line ) close $fid ### set up the menu frame .disc menu .disc.manu_menu -tearoff 0 foreach man $manufacturers { .disc.manu_menu add command -label "$man" -command {set disc_manufa +cturer $man} } pack [label .disc.manu_label -text "Manufacturer" -relief raised] -sid +e left bind .disc.manu_label <1> {tk_popup .disc.manu_menu %X %Y} ### here's the actual factual entry box entry .disc.manu_entry -width 30 -textvariable disc_manufacturer pack .disc.manu_entry -side left
The .txt file is just a simple list:
Capitol Columbia Edison RCA Victor Victrola
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: creating a Tk popup menu from a file
by Anonymous Monk on Sep 15, 2009 at 10:35 UTC |