Listbox与Listbox with key的区别

标准解释:

Listbox
Visualization as listbox in which a list of entries is displayed with one short description each.

Listbox with key
Visualization as listbox whose entries display both the key and the description.

 

分析:很多情况下我们难以找到它们之间的区别,是因为我们在GUI的全局设置里设置了以下这个选项

1

 

 

 

 

 

 

而当你把这个选项去掉的话,你就会发现使用LISTBOX仅显示值的描述,而使用LISTBOX WITH KEY 不仅显示值的描述,还显示值本身。

如下所示:

LISTBOX:

2

 

 

 

 

LISTBOX WITH KEY:
3




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过更改 `Completer` 类的 `_listbox` 方法来实现将点击选择改为双击选择。具体实现代码如下: ``` python import tkinter as tk from tkinter import ttk class DoubleClickCompleter(ttk.Entry): def __init__(self, master, options=[], **kwargs): super().__init__(master, **kwargs) self.options = sorted(options, key=str.lower) if options else [] self.current_options = [] self._listbox = None self._listbox_visible = False self._selected_index = None self.bind("<KeyRelease>", self._on_key_release) self.bind("<Return>", self._on_return) self.bind("<Button-1>", self._on_click) self.bind("<Double-Button-1>", self._on_double_click) def _on_key_release(self, event): """ Event handler for key releases. Updates the listbox with the current options. """ self.current_options = [ option for option in self.options if self.get().lower() in option.lower() ] self._update_listbox() def _on_return(self, event): """ Event handler for the return key. Inserts the selected option into the entry widget. """ if self._listbox_visible: self.delete(0, tk.END) self.insert(0, self._listbox.get(tk.ACTIVE)) self._listbox_visible = False self._listbox.destroy() self.icursor(tk.END) def _on_click(self, event): """ Event handler for mouse clicks. Hides the listbox if it is visible. """ if self._listbox_visible: self._listbox_visible = False self._listbox.destroy() def _on_double_click(self, event): """ Event handler for double mouse clicks. Inserts the selected option into the entry widget. """ if self._listbox_visible: self.delete(0, tk.END) self.insert(0, self._listbox.get(tk.ACTIVE)) self._listbox_visible = False self._listbox.destroy() self.icursor(tk.END) def _update_listbox(self): """ Updates the listbox with the current options. """ if not self.current_options: if self._listbox_visible: self._listbox_visible = False self._listbox.destroy() else: if not self._listbox_visible: self._listbox = tk.Listbox( master=self.master, height=len(self.current_options), exportselection=False, ) self._listbox.bind("<Button-1>", self._on_click) self._listbox.place( x=self.winfo_x(), y=self.winfo_y() + self.winfo_height(), ) self._listbox_visible = True self._listbox.delete(0, tk.END) for option in self.current_options: self._listbox.insert(tk.END, option) ``` 在这个实现中,我们新增了一个 `_on_double_click` 方法,用于处理双击事件。同时在 `_on_click` 中也需要加入判断,避免在双击时出现不必要的操作。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值