简体   繁体   English

隐藏并显示ttk。组合框下拉列表

[英]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. 情况:当我使用鼠标按钮单击ttk.Combobox的“向下箭头”时,标准行为是显示一个下拉列表。 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 . 问题:不使用鼠标或键盘,即使用计算机编程,如何隐藏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. 我知道w.event_generate("<Down>")命令可用于对ttk.Combobox进行编程以显示其下拉列表。 But how do I achieve the opposite? 但是我要如何做到相反呢? That is, how can I use the same w.event_generate() command to hide the dropdown list? 也就是说,如何使用相同的w.event_generate()命令隐藏下拉列表? Or what other tkinter command should I use to achieve what I want? 或者我应该使用其他什么tkinter命令来实现我想要的?

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. 使用"combobox_widget_object.event_generate('<Button-1>')"会导致组合框下拉列表显示。 Event '<Button-1>' appears to be inherently defined to cause this behavior. 事件'<Button-1>'似乎是固有定义的,以导致此行为。
  2. Running 2 of this command back to back do not lead to the showing and hiding of the combobox dropdown list. 连续运行此命令中的2不会导致组合框下拉列表的显示和隐藏。 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. "combobox_widget_object.after(delay_ms, callback=None, *args)"方法可用于指示组合框在一定时间延迟后运行功能。 That function should contain the 该功能应包含
    "combobox_widget_object.event_generate('<Button-1>')" method to cause the hiding of the dropdown list. "combobox_widget_object.event_generate('<Button-1>')"方法导致隐藏下拉列表。

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() . 更新:或者,要隐藏组合框下拉列表,可以使用命令source.event_generate('<Escape>')代替函数def _source_delayed_clicked() source.event_generate('<Button-1>')定义的def _source_delayed_clicked() source.event_generate('<Button-1>')命令。 def _source_delayed_clicked() This simulates pressing the keyboard "Esc" key. 这模拟了按下键盘的"Esc"键。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM