Python Forum
[Tkinter] Justify the treeview to the last entry
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Justify the treeview to the last entry
#1
Thankyou in advance for any assistance...
I have a ttk.Treeview that is populated from a sqlite3 database, it has a vertical scrollbar, the scrollbar scrolls from the first record to the last record with no problem. What I would like to happen is when a new record is added I want the treeview to show the newest record at the bottom of the table without grabbing the scollbar and moving it manually to reveal the latest record. I have tried (tree.yview_moveto(1)) also (tree.yview_moveto(1.0)) also (tree1.configure(tree.yview_moveto(1))). I have placed these in the function that that populates the treeview and they do nothing and they do not create any errors. I believe it has to be something simple that I am overlooking Confused

        def DisplayData():
                conn = sqlite3.connect("service.db")
                cur = conn.cursor()
                cur.execute("SELECT * FROM ticket")
                result = cur.fetchall()
                if len(result) !=0:
                        tree1.delete(*tree1.get_children())
                        for row in result:
                                tree1.insert('', tk.END, values=row)
                                tree1.yview_moveto(1.0) # not doing anything and neither does the following 2 lines
                                #tree1.configure(tree1.yview_moveto(1.0))
                                #tree1.yview("moveto", 1)            
                #conn.commit()
                conn.close()

                # this hilites the last entry but does not scroll to the focused line
                #tree1_count = tree1.get_children()[-1]
                #tree1.selection_set(tree1_count)
                #tree1.focus(tree1_count)
    
                return
Reply
#2
try tree1.update_idletasks()
after you have updated the queue
Reply
#3
Thankyou for the reply but I'm sorry to say that the suggestion of "update_idletasks" did not work. I tried it at the end of the for loop, tried it outside of the for loop and I tried it at the end of the Display function with no luck. I am really at a loss here.
Reply
#4
Even though I imagined that there was a way to automatically scroll down to the last item in the treeview I have found a work around that actually suits the situation quite well.

In the insert (tree1.insert('', tk.END, values=row)) change tk.END to 0 and the tree is populated in reverse order always putting the last item inserted at the top with no need to scroll down.

I am still interested to find a solution to the original question though.
Reply
#5
This is probably a better solution to the problem. If new items are important to see, they should probably appear at the top of the list. If they aren't important then you shouldn't scroll the view to display them.

I was able to scroll to the bottom, using yview.move_to(), but it only scrolled down every other time I added an item to the list.
Reply
#6
Well that is more than I was able to do, I tried your solution but still no luck. is it possible to see a snippit of code that you used, was it in the for loop or outside
Reply
#7
Use see(item) to see an item
import tkinter as tk
import tkinter.ttk as ttk

def bud():
    i = len(leafs)
    leafs.append(tree.insert('', tk.END, values=(i, i**2)))
    tree.see(leafs[-1])

leafs = []
root = tk.Tk()
tree_frame = tk.Frame(root)
tree_frame.pack()

tree = ttk.Treeview(tree_frame, columns=('A', 'A2'), height=5)
tree.heading('A', text='A', anchor='center')
tree.heading('A2', text='A**2', anchor='center')
tree.pack(side=tk.LEFT)

yscroll = ttk.Scrollbar(tree_frame,  orient=tk.VERTICAL, command=tree.yview)
yscroll.pack(side=tk.LEFT, fill='y')
tree.configure(yscroll=yscroll.set) 

tk.Button(root, text='Push Me', command=bud).pack()
root.mainloop()
MrP likes this post
Reply
#8
this is exactly what I was trying to accomplish and I was so close, I never thought to use 'tree.see()'.
As the young folks say "you da man!" ... or at least they use to say it. Big Grin
I would say this answers my question completely. Thankyou!!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] [split] Is there a way to embed a treeview as a row inside another treeview? CyKlop 5 3,305 Oct-20-2021, 12:14 AM
Last Post: CyKlop
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 4,427 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] Not getting entry values on button click & treeview not updating ? swanysto 4 6,966 May-10-2019, 04:16 PM
Last Post: swanysto
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 3,457 Mar-18-2019, 05:36 PM
Last Post: woooee

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020