【问题标题】:How to reset selection order in tkinter.Listbox?如何在 tkinter.Listbox 中重置选择顺序?
【发布时间】:2021-07-22 23:51:42
【问题描述】:

假设我有一个带有列表框的窗口。将项目插入我的列表框后,我想用向下箭头键移动选择,按下它后我看到选择移动到列表中的最后一个项目。

为什么按向下箭头会选择列表中的最后一个元素而不是第二个?需要做什么来恢复正确的选择顺序?

示例应用

from tkinter import *


root = Tk()
root.bind('<Escape>', lambda e: root.quit())
listbox = Listbox(root)
listbox.insert(END, *[i for i in range(20)])
listbox.pack()
listbox.focus()
listbox.selection_set(0)

# now, changing selection with down arrow key goes to last element on the list not second one

root.mainloop()

【问题讨论】:

  • 当我按下向下箭头键时,它不会选择列表中的最后一个元素

标签: python tkinter tk


【解决方案1】:

如果你向下滚动,你会注意到最后一个项目是“激活的”,你也需要激活你的第一个项目,所以:

listbox.selection_set(0)
listbox.activate(0)

【讨论】:

    猜你喜欢
    • 2014-01-16
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多