intractable(?) Tkinter scrollbar problem

Greg McFarlane gregm at iname.com
Wed Jun 2 22:29:02 EDT 1999


In private mail, someone suggested adding a delay before actually
mapping/unmapping the scrollbar.  This fix stops the continuous
mapping and unmapping of the scrollbars.  The change was to the
AutoScrollbar class, but I include the whole script below if you want
to try it out. 

The problem with this fix is that the length of the delay is
significant and may be different depending on your window system or
network delays.  For example, on my system (Solaris host, with X
server running locally), a delay of 100ms works well.  If I reduce
this to 10ms, the window resizes twice before settling.  If I reduce
the time even more, the window resizes more times before settling,
until I get to 1ms when the resizing is continuous.

It's not a good solution, but the best I've seen so far :(

Has anyone else seen this problem?

======================================================================
# Tkinter-only script which demonstrates continuous flashing
# (mapping/unmapping) of dynamic scrollbars (autoscrollbars).
#
# When this script is run, the two scrollbars will be continuously
# mapped and unmapped and the window will continuously change size.
#
# Script modified from Mark C Favas, using Fredrik Lundh's
# AutoScrollbar.

import Tkinter

class AutoScrollbar(Tkinter.Scrollbar): 
    def set(self, lo, hi):
	# Delay 100ms before deciding to map or unmap.
	self.after(100,lambda s=self,l=lo,h=hi: s._set(l,h))

    def _set(self,lo,hi):
        print lo, hi
        if float(lo) <= 0.0 and float(hi) >= 1.0:
            self.tk.call("grid", "remove", self)
        else:
            self.grid()
        Tkinter.Scrollbar.set(self, lo, hi)


root = Tkinter.Tk()

frame = Tkinter.Frame(root)
frame.grid()
frame.columnconfigure(0, weight=1)
frame.rowconfigure(0, weight=1)

scrollFrame = Tkinter.Frame(frame)
scrollFrame.columnconfigure(1, weight=1)
scrollFrame.rowconfigure(1, weight=1)
scrollFrame.grid(sticky='news')

vscrollbar = AutoScrollbar(scrollFrame)
vscrollbar.grid(row=1, column=2, sticky='ns')
hscrollbar = AutoScrollbar(scrollFrame, orient='horizontal')
hscrollbar.grid(row=2, column=1, sticky='ew')

borderframe = Tkinter.Frame(scrollFrame)
borderframe.rowconfigure(0, weight=1)
borderframe.columnconfigure(0, weight=1)
borderframe.grid(row=1, column=1, sticky='news')

canvas = Tkinter.Canvas(borderframe,
        highlightthickness=0,
        borderwidth=0,
        background = 'red',
        width = 300,
        height = 200,
        scrollregion = (0, 0, 301, 200),
        yscrollcommand=vscrollbar.set,
        xscrollcommand=hscrollbar.set)
canvas.grid(sticky='news')
vscrollbar.config(command=canvas.yview)
hscrollbar.config(command=canvas.xview)

root.mainloop()
======================================================================

-- 
Greg McFarlane:    INMS Telstra Australia (gregm at iname.com)
Today's forecast:  Sunny, with occasional cloudy periods and a chance
		   of rain in some areas.




More information about the Python-list mailing list