OptionMenu modify drop down list width to match OptionMenu width OptionMenu modify drop down list width to match OptionMenu width tkinter tkinter

OptionMenu modify drop down list width to match OptionMenu width


while there is no way to explicitly set the width, if you really must use tkinter then it is possible to add hacky workarounds to pad these things out. and example of this would be:

import tkinter as tkfrom tkinter import font as tkFontdef resizer(event=None):    print("Resize")    widget = event.widget    menu = widget['menu']    req_width = widget.winfo_width()-10    menu_width = menu.winfo_reqwidth()    cur_label = menu.entrycget(0, "label")    cur_label = cur_label.rstrip() # strip off existing whitespace    font = tkFont.Font() # set font size/family here    resized = False    while not resized:        difsize = req_width - menu_width # check how much we need to add in pixels        tempsize = 0        tempstr = ""        while  tempsize < difsize:            tempstr += " " # add spaces into a string one by one            tempsize = font.measure(tempstr) #measure until big enough        menu.entryconfigure(0, label=cur_label + tempstr) # reconfigure label        widget.update_idletasks() # we have to update to get the new size        menu_width = menu.winfo_reqwidth() # check if big enough        cur_label = menu.entrycget(0, "label") # get the current label for if loop needs to repeat        if menu_width >= req_width: # exit loop if big enough            resized = Trueroot = tk.Tk()var = tk.StringVar()var.set('First')option = tk.OptionMenu(root, var, 'First', 'Second', 'Third')option.bind("<Configure>", resizer) # every time the button is resized then resize the menuoption.configure(indicatoron = False)option.pack(expand = True, fill = tk.X)root.mainloop()

this essentially just pads out the first menu item until the menu is big enough. however there does seem to be some discrepancy in the widths reported back by tkinter hence my req_width = widget.winfo_width()-10 offset near the top.

however this will not always be a perfect match size wise, while testing my a space seems to take 3 pixels of width, so it could be 1 or 2 pixels out at any time.