Based on code at: Problem with QTableWidget.scrollToItem functionality I created this in PyQt5:
def scrollforward():
# will scroll forward if not at last row
if self.rPointer!=self.recipeTable.rowCount():
self.rPointer = 1
self.Table.scrollToItem(self.Table.selectRow(self.rPointer), QAbstractItemView.PositionAtCenter)
I want to reproduce this behaviour in a Tkinter Treeview. I wrote the following with the cited code from StackOverflow, but I'm not sure how to scroll the selected row so it is visible onscreen. Can someone help me? Thank you very much.
import tkinter as tk
import tkinter.ttk as ttk
class App:
def moveDown(self):
leaves = self.tree.selection()
for i in reversed(leaves):
# self.tree.move(i, self.tree.parent(i), self.tree.index(i) 1) # continues to show the row, but also actually moves the row
# self.tree.see(i) # has no effect
# According to j_4321 on https://stackoverflow.com/questions/42154164/:
# 'You can use tree.yview_moveto(1) to display the bottom of the table. The yview_moveto method takes as argument the fraction of the total (scrollable) widget height that you want to be off-screen to the top.'
def __init__(self, master):
self.frame = ttk.Frame(master)
self.frame.pack(fill = tk.BOTH, expand = True)
cols = (('Col1',20),('Col2',20)) # /33290969/
self.tree = ttk.Treeview(self.frame, columns = [x[0] for x in cols], show='headings', height=5)
for col, wdth in cols:
self.tree.heading(col, text = col) # /45008580/
self.tree.column(col, width = wdth)
clist = [('A','1'),('B','2'),('C','3'),('D','4'),('E','5'),('F','6'),('G','7'),('H','8'),('I','9'),('J','10'),('K','11'),('L','12'),]
for i in range(0,len(clist)):
self.tree.insert('',tk.END, values=clist[i]) # pythontutorial.net /65716152/
self.tree.grid(row=0, column=0, rowspan=5, columnspan=1, sticky='nsew')
self.fwdbutton = ttk.Button(self.frame, text='>', command = self.moveDown)
self.fwdbutton.grid(row=30, column=20, rowspan=1, columnspan=1, sticky='nsew')
root = tk.Tk()
app = App(root)
root.mainloop()
CodePudding user response:
If you want to just scroll down one row, you should use the yview_scroll
method:
def moveDown(self):
self.tree.yview_scroll(1, "units")
CodePudding user response:
After trying various commands and especially the see command, that you mentioned Bryan, I finally hit upon the behaviour I wanted: to move to the next row and make sure it is displayed:
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/