Skip to content Skip to sidebar Skip to footer

Hide And Show Ttk.combobox Dropdown List

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

Solution 1:

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:

  1. Using "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.
  2. Running 2 of this command back to back do not lead to the showing and hiding of the combobox dropdown list. It still only SHOWS the dropdown list as with a single command.
  3. The "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 modulesimport 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>' eventdef_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.

Post a Comment for "Hide And Show Ttk.combobox Dropdown List"