import tkinter as tk
from tkinter import ttk
def jump_to_first(event):
last = tree.get_children()[-1]
if tree.focus() == last:
first = tree.get_children()[0]
tree.selection_set(first) # move selection
tree.focus(first) # move focus
tree.see(first) # scroll to show it
return "break" # don't send event to TreeView
def jump_to_last(event):
first = tree.get_children()[0]
if tree.focus() == first:
last = tree.get_children()[-1]
tree.selection_set(last) # move selection
tree.focus(last) # move focus
tree.see(last) # scroll to show it
return "break" # don't send event to TreeView
root = tk.Tk()
tree = ttk.Treeview(root, selectmode="browse")
tree.pack()
for x in range(1, 21):
print(tree.insert('', 'end', text=str(x)))
tree.bind('<Down>', jump_to_first)
tree.bind('<Up>', jump_to_last)
root.mainloop()
您可以
bind()
函数在TreeView
中键入Down
(Up
),这将检查您是否在最后(第一)行并跳到第一(最后)行。它必须移动选择、移动焦点、滚动窗口和阻止事件,以便TreeView不会使用此键移动到下一行(上一行)相关问题 更多 >
编程相关推荐