Tkinter OptionMenu limit? Tkinter OptionMenu limit? tkinter tkinter

Tkinter OptionMenu limit?


Bind StringVar and function for your tk.OptionMenu.The function will receive the value you select in the dropdown menu.(You also need to use monospace font,)So I think you could get the selected value by the passed value.And use StringVar to set the display value.Like:

import tkinter as tkwidth = 10def strip_str(val):    selected_value = var.get()    var.set(selected_value[:width] + ("" if len(selected_value) <= width else "..."))    print(val)r = tk.Tk()r.option_add("*font", ("Consolas", 10))value_list = ["arc (arc, chord, or pieslice)","bitmap (built-in or read from XBM file)","image (a BitmapImage or PhotoImage instance)","line","oval (a circle or an ellipse)","polygon","rectangle","text","window"]var = tk.StringVar()menu = tk.OptionMenu(r, var, *value_list, command=strip_str)menu.pack()r.mainloop()

PS: The value in the OptionsMenu has been changed, though, the variable val is the value you selected before.Just handling val in the function is okay.


For those looking for an answer how to do it with OptionMenu:

add: width=10, anchor='w') to the config.

This will do it with OptionMenu


You could use a ttk Combobox instead of the option menu.

import tkinter as tkfrom tkinter import ttkroot = tk.Tk()cmb = ttk.Combobox(root, values=['Hello World!', 'Goodbye World!'])cmb.grid()

This will be displayed like so.

Original

cmb.configure(width=10)  # Can also set this when you create the combobox

Now it is cut off.

After changing the width