【问题标题】:Tkinter - How to go to the top of a Treeview when pressing down at the bottom, and vice versa?Tkinter - 在底部按下时如何转到Treeview的顶部,反之亦然?
【发布时间】:2020-01-05 22:57:43
【问题描述】:
tree = ttk.Treeview(root, selectmode="browse")
tree.pack()

当用户在已经选择了最后一项的情况下按下向下键时,我需要它来选择顶部的项目,向上箭头也是如此。谢谢!

【问题讨论】:

  • 您是否阅读过treeview 文档并尝试过什么?滚动的方法都有记录。
  • 我看不出有什么可以做到这一点。你能帮忙吗?

标签: python python-3.x tkinter treeview


【解决方案1】:

您可以通过bind() 函数在TreeView 中键入Down (Up),这将检查您是否在最后(第一)行并跳转到第一(最后)行。它必须移动选择、移动焦点、滚动窗口和阻止事件,因此 TreeView 将不会使用此键移动到下一个(上一个)行。

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

【讨论】:

    猜你喜欢
    • 2019-06-22
    • 2017-06-28
    • 1970-01-01
    • 2014-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多