隐藏并显示ttk.组合框下拉列表 [英] Hide and show ttk.Combobox dropdown list

查看:59
本文介绍了隐藏并显示ttk.组合框下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

情况:当我使用鼠标按钮单击 ttk.Combobox 的向下箭头"时,标准行为是显示一个下拉列表.第二次单击向下箭头时,组合框下拉列表将变为隐藏.

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.

问题:不使用鼠标或键盘,即使用计算机编程,如何隐藏 ttk.Combobox 的公开下拉列表.我知道 w.event_generate(< Down>") 命令可用于对 ttk.Combobox 进行编程以显示其下拉列表.但是我该如何实现相反的目标呢?也就是说,如何使用相同的 w.event_generate()命令隐藏下拉列表?还是我应该使用其他什么tkinter命令来实现我想要的?

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?

推荐答案

我对此问题进行了几次尝试,终于找到了一种通过编程隐藏组合框下拉列表的方法.我的代码如下所示.

I made several attempts at this question and finally found a way to hide the combobox droplist via programming. My code is shown below.

观察:

  1. 使用"combobox_widget_object.event_generate('< Button-1>')" 可以使组合框下拉列表显示.事件'< Button-1>'似乎是固有地定义为导致这种行为.
  2. 连续执行此命令的2个不会导致显示并隐藏组合框下拉列表.它仍然只显示下拉列表像使用单个命令一样列出.
  3. "combobox_widget_object.after(delay_ms,callback = None,* args)" 方法可用于指示组合框运行功能经过一定的时间延迟.该函数应包含
    "combobox_widget_object.event_generate('< Button-1>')" 方法导致隐藏下拉列表.
  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.

代码:

# 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)

更新:或者,要隐藏组合框下拉列表,请使用以下命令可以使用 source.event_generate('< Escape>')代替函数中定义的 source.event_generate('< Button-1>')命令 def _source_delayed_clicked().这模拟按下键盘的"Esc" 键.

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.

这篇关于隐藏并显示ttk.组合框下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆