【问题标题】:Python Tkinter Treeview Shift-Up binding is one iteration behind inputPython Tkinter Treeview Shift-Up 绑定是输入后面的一次迭代
【发布时间】:2019-09-01 17:49:47
【问题描述】:

我还希望能够使用<Shift-Up> 键来选择树视图中的项目。没有给出错误,但选择晚了一个元素。在我的代码中,我希望选择与焦点相同,但选择最终位于列表下方的元素上。

我的猜测是,Arrow-Up 键的默认绑定是在我绑定之后运行的。

我已经搜索了一个虚拟事件来替换绑定中的 <Up> - 与 <<TreeviewSelect>> 的功能相似,而不是 <ButtonPress-1> - 但没有运气。

知道如何在按下<Shift-Up> 时同步选择和焦点吗?

注意:树的选择模式设置为无,因为在我的主应用程序中,我需要进行的选择与默认设置略有不同。

import tkinter as tk
from tkinter import ttk
class Treeview_Select:
    def __init__(self, tree):
        tree.bind('<Shift-Up>', self.ShiftUp, add='+')
    def ShiftUp(self, event):
        if event.widget.index(event.widget.focus()) is not '':
            print(event.widget.index(event.widget.focus()))
            event.widget.selection_set(event.widget.focus())
app = tk.Tk()
tree = ttk.Treeview(app)
v_scrollbar = ttk.Scrollbar(app, orient='vertical', command=tree.yview)
tree.config(selectmode='none', yscrollcommand=v_scrollbar.set)
tree.grid(row=0, column=0, sticky='nesw')
v_scrollbar.grid(row=0, column=1, sticky='nes')
Treeview_Select(tree)
for i in range(14):
    tree.insert("", 'end', text=i)
app.mainloop()

【问题讨论】:

标签: python tkinter treeview


【解决方案1】:

您应该使用tree.prev(item) 进行此类操作。如果要实现&lt;Shift-Down&gt;,请使用tree.next(item)

def ShiftUp(self, event):
    if event.widget.index(event.widget.focus()) is not '':
        previous = event.widget.prev(event.widget.focus()) #use current focus to get previous one
        event.widget.selection_set(previous)
        event.widget.see(previous) #make sure the new selection is shown

【讨论】:

    猜你喜欢
    • 2015-04-25
    • 2021-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-08
    • 1970-01-01
    • 2021-09-15
    相关资源
    最近更新 更多