pythontkintertkinter.optionmenu

Tkinter OptionMenu: How to configure font size of drop down list?


I have an option menu list with a lot of entries to be used on a touch screen device. I am able to change the font size of the selected category with PopMenue.config(font=[something]), but when selecting the drop down menu the entries appear in the default small font:

Example Pic - red frame surrounds text to increase font

screenshot of an option menu's dropdown menu using smaller font than the option menu itself

How can I amend the font size from the drop down menu entries (red frame)?

Code snippet:

helv36 = tkFont.Font(family='Helvetica', size=30, weight=tkFont.BOLD)
popupMenu.config(font=helv36)

Solution

  • You have to use the nametowidget() method to get the widget object corresponding to the dropdown menu widget, then set its configuration.

    Here's a runnable example:

    import tkinter as tk
    import tkinter.font as tkFont
    
    root = tk.Tk()
    root.geometry('300x200')
    
    helv36 = tkFont.Font(family='Helvetica', size=36)
    options = 'eggs spam toast'.split()
    selected = tk.StringVar(root, value=options[0])
    
    choose_test = tk.OptionMenu(root, selected, *options)
    choose_test.config(font=helv36) # set the button font
    
    helv20 = tkFont.Font(family='Helvetica', size=20)
    menu = root.nametowidget(choose_test.menuname)  # Get menu widget.
    menu.config(font=helv20)  # Set the dropdown menu's font
    choose_test.grid(row=0, column=0, sticky='nsew')
    
    root.mainloop()
    

    Here's are two screenshots showing the default vs modified dropdown menu text sizes:

    screenshot of dropdown menus