Hi,

On Tue, 20 Sep 2022 08:05:36 +0000
Massimo POZZONI via Tkinter-discuss <tkinter-discuss@python.org> wrote:

(...)
> My problem is that I need an event that is triggered when the overall
> combox widget is left, which is not the FocusOut, because when I open
> the drop-down menu I am still using the widget, and it is not the
> <<ComboboxSelected>> because if I left the widget after writing inside
> the associated Entry, the event is not triggered because no selection
> from list happens.
>
> Is there any way to have an event that is triggered when the overall
> combobox is left, but not when the drop-down list is opened?

maybe a workaround might help.
As far as I know there is no simple way to directly access ttk subwidgets
from tkinter, but with a simple trick you could check if the focus went
from the combobox's entry to the drop down list, as in this example:

####################################################

from tkinter import *
from tkinter import ttk

root = Tk()
b = Button(root, text='Hi')
b.pack(padx=100, pady=30)

v = StringVar(value='foo')
# define a unique widget name for the combobox
c = ttk.Combobox(root, name='unique_name',
                    textvariable=v, values=('foo', 'bar', 'bla'))
c.pack(pady=30)

def do_something():
    print('Doing something...')

def on_focus_out(event):
    # check if the drop-down list has keyboard focus
    if 'unique_name.popdown' in str(root.tk.call('focus')):
        print('Drop-down list opened.')
    else:
        do_something()

c.bind('<FocusOut>', on_focus_out)
root.mainloop()

####################################################

Alternatively one could skip that uinque-name thing and use the fact that
focus_get() crashes when the drop down list has focus, with a modified
event callback:

def on_focus_out(event):
    try:
        c.focus_get()
        do_something()
    except KeyError:
        pass

Not sure about this one, maybe it might fail if the focus goes from the
combobox directly to some other ttk subwidget? In the
simplified example program above both versions seem to work.

Have a nice day,

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

Every living thing wants to survive.
                -- Spock, "The Ultimate Computer", stardate 4731.3
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
https://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to