7

I'm using a listbox (with scrollbar) for logging:

self.listbox_log = Tkinter.Listbox(root, height = 5, width = 0,)
self.scrollbar_log = Tkinter.Scrollbar(root,)

self.listbox_log.configure(yscrollcommand = self.scrollbar_log.set)
self.scrollbar_log.configure(command = self.listbox_log.yview)

Now, when I do:

self.listbox_log.insert(END,str)

I want the inserted element to be selected. I've tried:

self.listbox_log.selection_anchor(END)

but that doesn't work... Please suggest a solution...

2 Answers 2

17

AFAIK the ScrollBar widget doesn't have an auto-scroll feature, but it can be easily implemented by calling the listBox's yview() method after you insert a new item. If you need the new item to be selected then you can do that manually too using the listbox's select_set method.

from Tkinter import *

class AutoScrollListBox_demo:
    def __init__(self, master):
        frame = Frame(master, width=500, height=400, bd=1)
        frame.pack()

        self.listbox_log = Listbox(frame, height=4)
        self.scrollbar_log = Scrollbar(frame) 

        self.scrollbar_log.pack(side=RIGHT, fill=Y)
        self.listbox_log.pack(side=LEFT,fill=Y) 

        self.listbox_log.configure(yscrollcommand = self.scrollbar_log.set)
        self.scrollbar_log.configure(command = self.listbox_log.yview)

        b = Button(text="Add", command=self.onAdd)
        b.pack()

        #Just to show unique items in the list
        self.item_num = 0

    def onAdd(self):
        self.listbox_log.insert(END, "test %s" %(str(self.item_num)))       #Insert a new item at the end of the list

        self.listbox_log.select_clear(self.listbox_log.size() - 2)   #Clear the current selected item     
        self.listbox_log.select_set(END)                             #Select the new item
        self.listbox_log.yview(END)                                  #Set the scrollbar to the end of the listbox

        self.item_num += 1


root = Tk()
all = AutoScrollListBox_demo(root)
root.title('AutoScroll ListBox Demo')
root.mainloop()
2
  • You can also use the listbox "see" command, though when viewing the end element the effect is the same. Commented Sep 13, 2010 at 12:47
  • thanks, I used self.listbox_log.insert(END,str) size = len(self.listbox_log.get(-1, END)) self.listbox_log.yview_scroll(size,"units") your way is much more elegent.
    – lalli
    Commented Sep 14, 2010 at 3:16
2

try to do it in this way. (I have copied from another question: How to auto-scroll a gtk.scrolledwindow?) It works fine for me.

def on_TextOfLog_size_allocate(self, widget, event, data=None):
    adj = self.scrolled_window.get_vadjustment()
    adj.set_value( adj.upper - adj.page_size )
2
  • 1
    This won't work on a listbox because there is no get_vadjustment method, nor does the original question mention anything about a widget named scrolled_window. Are you sure you posted this answer to the right question? Commented Nov 30, 2012 at 3:23
  • I use pygtk but not tkint, sorry for the mistake. Commented Dec 10, 2012 at 2:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.