Is there a way to find out if the Tkinter optionmenu dropdown is active?

I am writing a Tkinter program where I have an options menu with other user interface elements. I have a requirement that if the dropdown optionmenu remains active for a certain time (say 1 minute), I need to close the drop-down menu and cancel the selection. I was wondering if there is a way to find out if the optionmenu drop-down menu is enabled. I looked at this specific link, but could not find any specific method for what I needed.

+4
source share
2 answers

Tracking whether a menu is closed or open is easily accomplished with a command and click binding. The hard part makes the menu close. Either a mouse click, or a mouse click, or pressing Escape will do this. But the generation of events does not occur. The following #commented_out bits represent failed experiments.

import tkinter as tk

root = tk.Tk()
n = 2
after_id = None
menu_open = False
omvar = tk.StringVar(root)

def timeout():
    print('timeout called')
    omvar.set('')
    #root.event_generate('<Key-Escape>', keysym='Escape', keycode=9)
    #om.destroy()
    #root.event_generate('<FocusIn>')
    #root.focus_get()
    #root.event_generate('<Button-1>', x=10, y=20)
    root.update()


def open(event):
    print('open called')
    global after_id, menu_open
    after_id = root.after(n*1000, timeout)
    menu_open = True

def close(val):
    print('closed by', val)
    global after_id, menu_open
    root.after_cancel(after_id)
    after_id = None
    menu_open = False

om = tk.OptionMenu(root, omvar, 'a', 'b', 'c', command=close)
om.bind('<Button-1>', open)
om.pack()
root.mainloop()

The MenuButton + menu will look almost the same and could be simpler. Do you think that?

+1
source

(: ddo = 0) , ddo = 1 ( ddo == 1, ddo = 0) ddo == 1 1 , 1 , ddo == 1, selectbox (, ) , . :

def OnClick(event):
    if(ddo == 0):
        ddo = 1
        startTimerForOneSec(whenFinished=finishTimer())
    else:
        ddo = 0

    def finishTimer():
        if(ddo == 1):
            focusOneSomeOtherElements()
            doSomeThingElse()
+1

All Articles