Listbox + scrollbar refresh problem

eg ergeorge at worldnet.att.net
Tue Dec 18 12:06:08 EST 2001


Ok, I have a listbox & scrollbar tied together as described in the
pythonware documentation.(http://www.pythonware.com/library/tkinter/introduction/x5453-patterns.htm)

I also have a polling method setup with after() function so that my
listbox is refreshed every few seconds

The problem is that I can't seem to preserve the listbox view over the
refresh.  I can use the listbox.see() function to make sure the
current selection is visible, but the scrolled position moves.

It seems I should be able to make this happen by saving the scrollbar
position with:

scroll=scrollbar.get()

before I clear & reload the listbox, and then restore it after the new
data is loaded:

scrollbar.set(scroll[0], scroll[1])

But it doesn't work!?!

I've pasted an example below.
Any help is appreciated!
Eric

#################################################################
#!/usr/bin/python2

import sys
import os
import glob
import time
import string
from Tkinter import *

class Example1:
	def __init__(self):
		self.root = root = Tk()

		self.root.minsize(300, 200)

		self.row = 0
		self.root.rowconfigure(0, weight=1)
		self.root.columnconfigure(0, weight=1)
		self.root.columnconfigure(1, weight=0)

		self.listbox = Listbox(root, selectmode=SINGLE)
		self.listbox.grid(column=0, sticky=E+W+N+S)

		self.scrollbar = Scrollbar(root, orient=VERTICAL)
		self.scrollbar.grid(row = self.row, column = 1, sticky=W+N+S)
		self.scrollbar.config(command=self.listbox.yview)

		self.listbox.config(yscrollcommand=self.scrollbar.set,
font='courier')

		self.load_data()
		
		self.poll(5000)
		
		root.mainloop()
		
	def poll(self, interval):
		self.load_data()
		self.root.after(interval, self.poll, interval)
	
	def load_data(self):
		select = self.listbox.curselection()
		
		scroll = self.scrollbar.get()

		print "Scroll: ", scroll
		
		self.listbox.delete(0, END)
			
		for i in range(20):
			self.listbox.insert('end', `i`)

		if len(select):
			self.listbox.select_set(int(select[0]))
			self.listbox.see(int(select[0]))

		self.scrollbar.set(scroll[0], scroll[1])
		
		
x = Example1()



More information about the Python-list mailing list