Tkinter Menubutton items disapper

Viewed 83

I have a vallid code

from tkinter import *

top = Tk()

mb=  Menubutton ( top, text="CheckComboBox", relief=RAISED )
mb.grid()
mb.menu  =  Menu ( mb, tearoff = 0 )
mb["menu"]  =  mb.menu

Item0 = IntVar()
Item1 = IntVar()
Item2 = IntVar()

mb.menu.add_checkbutton ( label="Item0", variable=Item0)
mb.menu.add_checkbutton ( label="Item1", variable=Item1)
mb.menu.add_checkbutton ( label="Item2", variable=Item2)

mb.pack()
top.mainloop()

When I click to "CheckComboBox" I see list of items. Then I click to "Item1" and list of items disapper and I have to click to "CheckComboBox" many times if I want to choose a few items.

I want "list of times" don't disappeares after click to Item.

enter image description here

2 Answers

Solved it:

import tkinter as tk

root = tk.Tk()
root.geometry("%ix%i" % (50, 50))

def raise_menubutton(*args):
    # Open the menu again. Taken from:
    # https://stackoverflow.com/a/38676204/11106801
    menubutton.menu.post(menubutton.winfo_rootx(), 
                menubutton.winfo_rooty() + menubutton.winfo_height())

menubutton = tk.Menubutton(root, text="CheckComboBox", relief="raised")
menubutton.grid()
menubutton.menu = tk.Menu(menubutton, tearoff=0)
menubutton["menu"] = menubutton.menu

menubutton.menu.bind("<Button-1>", raise_menubutton)

Item0 = tk.IntVar()
Item1 = tk.IntVar()
Item2 = tk.IntVar()

# Bind to variables changing
Item0.trace("w", raise_menubutton)
Item1.trace("w", raise_menubutton)
Item2.trace("w", raise_menubutton)

menubutton.menu.add_checkbutton(label="Item0", variable=Item0)
menubutton.menu.add_checkbutton(label="Item1", variable=Item1)
menubutton.menu.add_checkbutton(label="Item2", variable=Item2)

menubutton.pack()

root.mainloop()

Works on Windows at least.

Menu behavior like this is defined by the OS / window manager, and tkinter doesn't provide any way to control it.

Related