Updated 2013-12-14 15:08:09 by Zipguy

tk_optionMenu with a long list, has scrolling edit

Zipguy 2013-12-12 - You can find out my email address by clicking on Zipguy.

I think that tk_optionMenu is a great command. It does give you choices, will default to, whatever you choose, will set your variable, and show that, on the button. Slick.

Even better, if you have too many options to fit on the screen, it will fit them, and it has scrolling built-in, to that command.

I made a simple program which uses a list. It is a list of animals, unsorted. Here's what it looks like:

So, if you click on the button labelled 'Pick an Animal (unsorted)' (which is highlighted in red), you will see a screen like this:

Note that, without moving the mouse, it has filled the screen, with options, to choose. But, if you look closely, you will notice an arrow, pointing down, at the bottom of the screen:

On the screen above it is highlighted in red. If you click on that button, the screen will scroll down. If you do so, you will see a screen like this:

Once you've made a selection (I've selected 'Lepoard'), you will see a screen like this:

All that means, is that you have selected 'Lepoard', and that's the name of the animal, that shows on the button. The self-same button used to show 'Pick an Animal (unsorted)'.

Code edit

Here's the code that created thoose screens:
 # Written by zipguy on 2013-12/12
 # Shows you the joys of tk_optionMenu with a long list
 set alist [list {Tarantula} {Aardwolf} {African bush elephant} \
     {Amazon river dolphin} {American alligator} {Aardvark} {Tasmanian devil} \
     {Green anaconda} {Tiger} {Tiger shark} {American white pelican} \
     {Andean condor} {Turkey} {Arabian camel} {Asian elephant} {Atlantic salmon} \
     {Bahaman raccoon} {Bald eagle} {Banded pitviper} {Black wildebeest} \
     {Blue whale} {BobcatCapybara} {Caribou (reindeer)} {Cheetah} \
     {Common bottlenose dolphin} {Common chimpanzee} {Cougar} {Coyote} \
     {Dingo Eastern diamondback rattlesnake} {Elephant seal} {Elk} \
     {Emperor penguin Emu} {European otter} {Four-horned antelope} \
     {Giant anteater} {Giant panda} {Iguana} {Impala} {Snow leopard} {Snowy owl} \
     {Giraffe} {Golden hamster} {Golden-capped fruit bat} {Gray fox} {Wolf} \
     {Great spotted kiwi} {California condor} {California sea lion} \
     {Great white shark} {Greater dwarf lemur} {Gray heron} {Gray squirrel} \
     {Guinea baboon} {Guinea pig} {Hedgehog} {Hippopotamus} {Horse} {Sparrow} \
     {Sperm whale} {Spider monkey} {Spotted halibut} {Jackal} {Jaguar} \
     {Kangaroo rat} {Killer whale} {King cobra} {Koala bear} {Komodo dragon} \
     {Leatherback turtle} {Leopard} {Lion} {Marsh rabbit} {Mekong giant catfish} \
     {Nightingale} {Nine-banded armadillo} {North American beaver} \
     {Northern cardinal} {Northern flying squirrel} {Ocelot} {Orangutan} \
     {Ostrich} {Peregrine falcon} {Bee hummingbird} {Black rhinoceros} \
     {Black widow spider} {Polar bear} {Praying mantis} {Red kangaroo} \
     {Red panda} {Spotted hyena} {Spur-thighed tortoise} {Virginia opossum} \
     {Western gorilla} {White-backed vulture} {American bison} {American crow} \
     {American flamingo} {Wildcat} {Wolverine}]

 wm title . "tk_optionMenu with a long list"
 wm minsize . 300 60

 tk_optionMenu .foo1 myVar1 "Pick an Animal (unsorted)" 
 pack .foo1
 foreach x $alist { 
    .foo1.menu add radiobutton -label $x -variable myVar1 \
          -command "puts \"Current choice1=<$x>\""

 tk_optionMenu .foo2 myVar2 "Pick an Animal   (sorted)"
 pack .foo2
 foreach x [lsort -dictionary $alist] { 
    .foo2.menu add radiobutton -label $x -variable myVar2 \
          -command "puts \"Current choice2=<$x>\""

MG tk_optionMenu may not always be the best tool for long lists like this, though. It's often considered to be an "older" look than using, for instance, a ttk::combobox widget, which can also be more easily tweaked to provide more features (such as keyboard traversal of the list).

Zipguy 2013-12-14 - I'll look into ttk::combobox widget. However, I doubt it will automatically use all of the screen, on whatever platform tk_optionMenu's running, which tk_optionMenu determines automatically, and provides built-in scrollbuttons, at the bottom and top of the screen (rather than a full scrollbar). It may seem to be an "older" look, but that doesn't bother me at all. It does have useful features, with no coding at all, to support them.

I'm thinking of adding it to ML - Heavily Modified & Improved, to replace the 'Procedures' window, via a button, at the bottom of the screen, on the status frame (which is highlighted in red, on the screenshot below). The existing 'Procedures' window, does not remember the 'last selected' proc, as I think tk_optionMenu would, and scroll it to make sure the 'last selected' proc is visible. So I think it might be another option, to change what part of the file, you are editing, and viewing, currently. That would be more convenient for users. I think. I'll see what happens. It may cause a little coding. For example, if you add a couple of new procs, while editing, subsequently click on the 'Pick a Proc...' button, to save the currently selected proc, have to rebuild the menu buttons, by deleting the existing ones, adding them back in, and then resetting the variable to the 'last selected' proc. That does not sound like too much code for me. Building the 'Procedures' window, caused creating a lot more code, than this approach would.