Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Autoscroll on insert in Tkinter Listbox?

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...

like image 237
lalli Avatar asked Sep 13 '10 09:09

lalli


People also ask

How do I add a scrollbar to my listbox?

If you want to force a scrollbar in your ListBox, use the ScrollBar. VerticalScrollBarVisibility attached property. Setting this value to Auto will popup the scrollbar on an as needed basis.

How do I add data to a listbox in Python?

A listbox shows a list of options. You can then click on any of those options. By default it won't do anything, but you can link that to a callback function or link a button click. To add new items, you can use the insert() method.

Which method is used to add elements in listbox tkinter?

set () method of the vertical scrollbar.

Can we add scrollbar to label in tkinter?

We can use Tkinter's scrollbar and add it to our text widget. Now, after adding the scrollbar, we should be able to display larger texts properly. To create a scrollbar object, use tk. Scrollbar() and add it to our application!


1 Answers

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()
like image 76
volting Avatar answered Oct 27 '22 00:10

volting