Rob Ros T Rob Ros T - 3 months ago 27
Python Question

python tkinter.ttk combobox down event on mouseclick

I do not find a correct answer to my issue, despite intense research and a rather simple problem.
All I would like to do, is my comboboxes drop down when clicked on by 'Button-1'. But regardless of what I code, the combos don't behave as I wish.

following I prepared a simple code to demonstrate my problem:

from tkinter import *
import tkinter.ttk


def combo_events(evt):
if int(evt.type) is 4:
w = evt.widget
w.event_generate('<Down>')


root = Tk()
li = ('row 1', 'row 2', 'row 3')

combo1 = tkinter.ttk.Combobox(root, value=li)
combo2 = tkinter.ttk.Combobox(root, value=li)

combo1.bind('<Button-1>', combo_events)
combo2.bind('<Button-1>', combo_events)

combo1.pack()
combo2.pack()

root.mainloop()


Well, if I try this code, the combos do dropdown, but not as expected. So, I tried to add a bind of the 'FocusIn' event but that rather complicates the situation and inhibits a 'FocusOut' ...

Can any1 help me to achieve my goal?

ps: I know, that the combo will drop down by clicking on the frame of the widget, but to be more precise I would like to drop it, when clicking into it.

And by the way, where do I find a rather complete list of events a combobox can trigger?

thx for effort and answer.

Answer

Why are you using int(evt.type) is 4 instead of int(evt.type) == 4 ?

Applying this change it works for me.

Edit 1

First of all, thank you for explaining to us what you really want to have. Did not expect this from your initial question.

If you want to override the editing behaviour it is time to dig a little deeper.

The widget you click into inside the combobox is an entry widget. What you can do now is to define when your event shall be fetched inside the event chain. Will apply code soon.

Edit 2

To get it at the first mouse click:

w.event_generate('<Down>', when='head')

Why? Because default of Event Generate is to append the generated Event to the Event Chain (put it at its end, value = 'tail'). Changing to when='head' gives your desired behaviour.

Comments