Reputation: 8234
Situation: When I use the mouse button to click the "down-arrow" of a ttk.Combobox
it's standard behaviour is to show a dropdown list. When the down arrow is clicked on the second time, the combobox dropdown list will become hidden.
Using the keyboard. it is possible to show the combobox dropdown list by pressing the "down-arrow" once. Pressing the "down-arrow" further will scroll down the dropdown list to its end. Pressing the "up-arrow" repeatedly will scroll up the dropdown list until the highlight/selection reaches to the top of the dropdown list, but it does not finally hide the dropdown list.
Question: Without using the mouse or keyboard, that is, using computer programming, how can I hide an expose dropdown list of a ttk.Combobox
. I am aware that the w.event_generate("<Down>")
command can be used to program a ttk.Combobox
to show it's dropdown list. But how do I achieve the opposite? That is, how can I use the same w.event_generate() command to hide the dropdown list? Or what other tkinter command should I use to achieve what I want?
Upvotes: 1
Views: 4484
Reputation: 8234
I made several attempts at this question and finally found a way to hide the combobox droplist via programming. My code is shown below.
OBSERVATIONS:
"combobox_widget_object.event_generate('<Button-1>')"
can
cause the combobox dropdown list to show. Event '<Button-1>'
appears to be
inherently defined to cause this behavior."combobox_widget_object.after(delay_ms, callback=None, *args)"
method can be used to instruct the combobox to run a function
after certain time delays. That function should contain the"combobox_widget_object.event_generate('<Button-1>')"
method to cause the
hiding of the dropdown list.CODE:
# tkinter modules
import tkinter as tk
import tkinter.ttk as ttk
"""
Aim:
Create a combobox widget and use w.event_generate(sequence, sequence,**kw) to
simulate external stimuli to cause combobox dropdown list to show and hide.
Author: Sun Bear
Date: 16/01/2017
"""
# Function to activate combobox's '<Button-1>' event
def _source_delayed_clicked():
print ('\n def __source_delayed_clicked():')
print('Delayed 2nd simulation of external stimuli')
print('HIDE combobox Dropdown list. \n'
'IT WORKED!')
source.event_generate('<Button-1>')
root = tk.Tk()
source_var=tk.StringVar()
reference=['Peter', 'Scotty', 'Walter', 'Scott', 'Mary', 'Sarah']
# Create Main Frame in root
frame0 = ttk.Frame(root, borderwidth=10, relief=tk.RAISED)
frame0.grid(row=0, column=0, sticky='nsew')
# Create Combobox
source = ttk.Combobox(frame0, textvariable=source_var, values=reference)
source.grid(row=0, column=0, sticky='nsew')
# Simulate external stimuli using w.event_generate(sequence,**kw)
print('\n', '1st simulation of external stimuli using: \n'
' source.event_generate('"<Button-1>"') \n'
' SHOW Combobox Dropdown List.')
source.event_generate('<Button-1>')
#source.event_generate('<Button-1>') # running another similar command
# back to back didn't work
delay = 1000*6 # 6 seconds delay
source.after(delay, _source_delayed_clicked)
Update:
Alternatively, to hide the combobox dropdown list, the command
source.event_generate('<Escape>')
can be used in place of the source.event_generate('<Button-1>')
command defined in the function def _source_delayed_clicked()
. This simulates pressing the keyboard "Esc"
key.
Upvotes: 2