Custom Tkinter scrollbar

H

Hunter.lennon

I want to create a custom scrollbar using particular images, which
will then be placed on a canvas to control another window on the
canvas. Right now I am inheriting from scrollbar, but I do the
movement with custom functions. When I create it and put in into the
canvas with "canvas.create_window" a standard scrollbar shows in the
correct spot and my custom one is outside of the canvas.

All I have right now is something that moves like a scrollbar but has
no effect on other objects.

Can anyone give me some advice or point me to a guide for this? Is it
even possible? Can I create a widget that mimics a scrollbar, or
would that be more difficult? I have been unable to find anything
online and would appreciate any help.
 
J

jim-on-linux

I want to create a custom scrollbar using particular images, which
will then be placed on a canvas to control another window on the
canvas. Right now I am inheriting from scrollbar, but I do the
movement with custom functions. When I create it and put in into
the canvas with "canvas.create_window" a standard scrollbar shows
in the correct spot and my custom one is outside of the canvas.

All I have right now is something that moves like a scrollbar but
has no effect on other objects.

Can anyone give me some advice or point me to a guide for this? Is
it even possible? Can I create a widget that mimics a scrollbar,
or would that be more difficult? I have been unable to find
anything online and would appreciate any help.

Sounds to me that you want a navigation window for a chosen image.
Gimp imaging program has such a window. Check out how gimp works
with multiple images.

You might want to work with a toolbar that opens a navigation window
for each image.

For an explanation on how scrolling works check out a book by O'Reilly
written by Mark Lutz, Programming Python, look up programming
scrollbars.

jim-on-linux
http://www.inqvista.com
 
H

Hunter.lennon

I ended up giving up on doing the scrollbar as a separate class, which
is probably for the best. This is the pertinent code for what I
wanted, and it does what I need it to do. I found the canvas-moving-w-
mouse.py program helpful in getting started with this; some of the
code is directly from that example. I'm posting my code on the off
chance someone will find something of use in it.

#Constants for the scrollbar icon range.
self.UPPER_LIMIT = 160
self.LOWER_LIMIT = 334

#Prepare pictures
self.upArrow = tk.PhotoImage ("upArrow", file =
"upArrow.gif")
self.scrollIcon = tk.PhotoImage ("scrollIcon", file =
"scrollIcon.gif")
self.downArrow = tk.PhotoImage ("downArrow", file =
"downArrow.gif")



#Main Canvas
self.shell = tk.Canvas (parent, width = 388, height = 408,
borderwidth = - 2)
self.shell.create_image (0, 0, image = self.shell_image,
anchor = tk.NW)
self.shell.create_image (361, 9, image = self.exit_image,
anchor = tk.NW, tags = "Exit")

#Inner frame that contains a canvas.
#This is what is scrolled by the scrollbar.
self.masteryFrame = tk.Frame (parent, borderwidth = -2)
self.masteryCanvas = tk.Canvas (self.masteryFrame, width =
326, height = 218,
scrollregion =
(0,0,326,439), borderwidth = -2)
self.masteryCanvas.pack()
self.masteryFrame.pack()
self.masteryCanvas.create_image (0, 0, image =
"masteryFrame", anchor = tk.NW)
self.shell.create_window (22, 135, window =
self.masteryFrame,
width = 326, height = 218,
anchor = tk.NW)


#Scrollbar
self.shell.create_image (350, 136, image = self.upArrow,
anchor = tk.NW,
tags = "upArrow")
self.shell.create_image (357, 160, image =
self.scrollIcon, tags = "scroll")
self.shell.create_image (350, 343, image = self.downArrow,
anchor = tk.NW,
tags = "downArrow")

self.shell.tag_bind ("scroll", "<Button-1>",
self.mouseDown)
self.shell.tag_bind ("scroll", "<B1-Motion>",
self.mouseMove)
self.shell.tag_bind ("upArrow", "<ButtonRelease-1>",
self.stepUp)
self.shell.tag_bind ("downArrow", "<ButtonRelease-1>",
self.stepDown)
self.shell.tag_bind ("Exit", "<Button-1>", self.close)

self.shell.pack (side = tk.LEFT)



def mouseDown (self, event):
#Scrollbar Function
#Remember where the mouse went down"
self.lastx = event.x
self.lasty = event.y

def mouseMove (self, event):
#Scrollbar Function
#Whatever the mouse is over is automatically tagged as
current by tk.
#Only moves vertically.
#Keep the cursor in bounds:
if event.y >= self.UPPER_LIMIT and event.y <=
self.LOWER_LIMIT:
self.shell.move(tk.CURRENT, 0, event.y - self.lasty)
self.lasty = event.y
elif event.y < self.UPPER_LIMIT:
self.shell.coords(tk.CURRENT, 357, self.UPPER_LIMIT )
self.lasty = event.y
elif event.y > 334:
self.shell.coords(tk.CURRENT, 357, self.LOWER_LIMIT)
self.lasty = event.y
self.masteryCanvas.yview_moveto (self.scrollbarPosition())


def stepUp (self, event):
#Scrollbar Function
#Move up one row or to the top, whichever is less.
#43.5 causes the canvas to move one row.
next = self.shell.coords ("scroll")[1] - 43.5
if next >= self.UPPER_LIMIT:
self.shell.coords ("scroll", 357, next)
else:
self.shell.coords ("scroll", 357, self.UPPER_LIMIT)
self.masteryCanvas.yview_moveto (self.scrollbarPosition())

def stepDown (self, event):
#Scrollbar Function
#Move down one row or to the bottom, whichever is less.
#43.5 causes the canvas to move one row.
next = self.shell.coords ("scroll")[1] + 43.5
if next <= self.LOWER_LIMIT:
self.shell.coords( "scroll", 357, next)
else:
self.shell.coords( "scroll", 357, self.LOWER_LIMIT)
self.masteryCanvas.yview_moveto (self.scrollbarPosition())


def scrollbarPosition (self):
#Scrollbar Function that computes movement
#Return a value between 0 and .5 for moving the canvas.
yCoord = self.shell.coords ("scroll")[1]
length = self.LOWER_LIMIT - self.UPPER_LIMIT
current = yCoord - self.UPPER_LIMIT
current /= 2
#print current / length
return (current / length)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top