2

As simple as it sounds, I have an Option menu with some 5 character terms and one very long one. When the long option is selected, the window stretches out and it looks atrocious. Setting the width or sticky=EW only works if that width is greater than the length of the longest term.

Ideally, I'd like to show 15 characters max followed by a "..." if it is longer.

Any ideas? Thanks.

user3000724
  • 651
  • 3
  • 11
  • 22
  • Could you just check when adding each thing: "labels = [label[:15] + '...' if len(label) > 15 else '''' for label in labels" ? – en_Knight Mar 04 '16 at 19:33
  • While that is a great idea, the full name is still very important. I don't mind if the drop down menu is a bit longer once dropped, as long as it doesn't keep resizing the window. But when the 'item' is selected, it would be nice for it to be the 15 characters plus an ellipses. I figure I could use a simple data structure to store an original and abbreviated, but was hoping Tkinter might have had something built it. Thanks for the suggestion though. – user3000724 Mar 04 '16 at 21:09

1 Answers1

1

I think you're looking for the more powerful "Combobox" in ttk (also in standard python as simple extensions to Tk).

As effbot puts it

The option menu is similar to the combobox

For example:

from tkinter.ttk import Combobox # python 3 notation
combo = Combobox(root,values=['a','aa','aaaaa','aaaaaaa'],width=3)

It will simply cut off the elements that are too long

(If you're in python 2 it's slightly different to import ttk)

If you want a nice "..." to appear when cutting off entries, I think you're best bet is to

elements = ['a','aa','aaaaaaaa']
simple_values = [ e[:3] + ('...' if len(e) > 3 else '') for e in elements]
combo = Combobox(root,values=simple_values )

If you need to be able to map between them, use your favorite data structure or reference by index not value in the combobox

Community
  • 1
  • 1
en_Knight
  • 5,301
  • 2
  • 26
  • 46
  • That is absolutely perfect, thanks so much! I didn't try Python 3, but I was able to easily upgrade my OptionMenus for Comboboxes with the Python 2 example. – user3000724 Mar 08 '16 at 13:57