The Scale Widget

johngrayson at home.com johngrayson at home.com
Wed Mar 28 21:52:15 CEST 2001


Hans Kristian Ruud wrote:

> Is there a way to modify the scale widget so that the slider
> only can move in one direction?

Here's a programatic solution:

from Tkinter import *
import string

global scale, position

position = 100

def setHeight(canvas, heightStr):
    global position
    height = string.atoi(heightStr)

    if height < position:
        height = position
	if height > 250: height = 250
	position = height
	scale.set(position)

    height = height + 1
    position = height
    y2 = height - 30
    if y2 < 1:
        y2 = 1
    canvas.coords('poly',
                 15,20,35,20,35,y2,45,y2,25,height,5,y2,15,y2,15,20)
    canvas.coords('line',
                 15,20,35,20,35,y2,45,y2,25,height,5,y2,15,y2,15,20)

root = Tk()
root.title('Scale')

canvas = Canvas(root, width=50, height=50, bd=0, highlightthickness=0)
canvas.create_polygon(0,0,1,1,2,2, fill='cadetblue', tags='poly')
canvas.create_line(0,0,1,1,2,2,0,0, fill='black', tags='line')
scale = Scale(root, orient=VERTICAL, length=284, from_=0, to=250,
              tickinterval=50, command=lambda h,
              c=canvas:setHeight(c,h))
scale.grid(row=0, column=0, sticky='NE')
canvas.grid(row=0, column=1, sticky='NWSE')
scale.set(position)
root.mainloop()







More information about the Python-list mailing list