在 tkinter 的树视图中向上移动

问题描述 投票:0回答:1

我发现下面显示的这个功能对于在树视图中向下移动非常有用,但我在这里问是否有向上移动功能。

def moveDown(self):
    curSelection = self.tree.selection() # current row - from /68508694/
    nextSelection = self.tree.next(curSelection) # next row
    self.tree.selection_set(nextSelection) # set the next row to be the current row
    self.tree.see(nextSelection) # make sure the current row is shown - from /10155153/
tkinter treeview
1个回答
0
投票

将其

nextSelection = self.tree.next
更改为
prevSelection = self.tree.prev
,您应该检查是否有空值,请参阅评论:

def moveUp(self):
    curSelection = self.tree.selection()  # Get the current selection
    if not curSelection:  # If no row is selected, exit
        return
    
    prevSelection = self.tree.prev(curSelection)  # Get the previous row
    if prevSelection:  # Check if there's a previous row
        self.tree.selection_set(prevSelection)
        self.tree.see(prevSelection) 
© www.soinside.com 2019 - 2024. All rights reserved.