Python Forum
Adding a sub menu to a ttk.OptionMrnue ?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding a sub menu to a ttk.OptionMrnue ?
#2
That is not a feature that option menu supports. You cannot add a submenu to one of the options, because the options are radio buttons. To have submenus you need the option to be menus.

OptionMenu is really a convenience class for making a menu button with a menu of radio buttons. Here's an example of making an OptionMenu-like thing that supports cascading.

https://stackoverflow.com/questions/2435...-be-nested

It wouldn't be difficult to use this code as a starting point for making your own CascadedOptionMenu class.

I don't see how such a thing could be used to select 2 options as it only has 1 value. The cascading being more a mechanism for organizing the values than adding structure to the result.

You could organize your options to get two values using a regular option menu like this:
import tkinter as tk

options = {
    "1 A": (1, "A"),
    "1 B": (1, "B"),
    "1 C": (1, "C"),
    "2 A": (2, "A"),
    "2 B": (2, "B"),
    "2 C": (2, "C"),
    "3": 3,
    "4": 4,
}

root = tk.Tk()
var = tk.StringVar(root, list(options)[0])
menu = tk.OptionMenu(root, var, *list(options))
menu.pack(padx=50, pady=50)
var.trace_add("write", lambda x, y, z: print(var.get(), options[var.get()]))
root.mainloop()
Reply


Messages In This Thread
Adding a sub menu to a ttk.OptionMrnue ? - by Robo - Apr-16-2024, 10:31 AM
RE: Adding a sub menu to a ttk.OptionMrnue ? - by deanhystad - Apr-16-2024, 08:19 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020