Bartłomiej 'furas' Burek
furas.pl
# prywatne notatki - Python, Linux, Machine Learning, etc.

Use down arrow to jump from last to first row in Treeview

It uses bind() to execute function when you press Down arrow in Treeview.

Function checks if you are in last row

last = tree.get_children()[-1]
if tree.focus() == last:

and moves focus and selection to first row, and it scrolls window to this row. It also sends string "break" so Treeview will not use this event to move from first to second row.

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

Similar function is for Up arrow to jump from first to last row.

import tkinter as tk
from tkinter import ttk

# --- functions ---

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

# --- main ---

root = tk.Tk()

tree = ttk.Treeview(root)
tree.pack()

tree.bind('<Down>', jump_to_first)
tree.bind('<Up>', jump_to_last)

# create many rows so it will have to scroll them 
for x in range(1, 21):
    tree.insert('', 'end', text='Row {:02}'.format(x))

root.mainloop()
Książki: python-dla-kazdego-podstawy-programowania python-wprowadzenie python-leksykon-kieszonkowy python-receptury python-programuj-szybko-i-wydajnie python-projekty-do-wykorzystania black-hat-python-jezyk-python-dla-hackerow-i-pentesterow efektywny-python-59-sposobow-na-lepszy-kod tdd-w-praktyce-niezawodny-kod-w-jezyku-python aplikacje-internetowe-z-django-najlepsze-receptury