Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Custom Tkinter scrollbar

Reply
Thread Tools

Custom Tkinter scrollbar

 
 
Hunter.lennon@gmail.com
Guest
Posts: n/a
 
      11-14-2007
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.

 
Reply With Quote
 
 
 
 
jim-on-linux
Guest
Posts: n/a
 
      11-15-2007
On Wednesday 14 November 2007 18:22, http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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

 
Reply With Quote
 
 
 
 
Hunter.lennon@gmail.com
Guest
Posts: n/a
 
      11-24-2007
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)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
tkinter modifying multiple widgets with one scrollbar spohle Python 1 06-24-2006 03:30 PM
Tkinter Scrollbar not working Dustan Python 8 01-03-2006 11:12 PM
Tkinter: scrollbar in grid-managed frame anx@io.us Python 1 11-16-2005 09:47 PM
Tkinter and Scrollbar codecraig Python 0 04-19-2005 03:17 PM
ScrollBar? Does it exist just WEB ScrollBar Control? Alex ASP .Net Web Controls 1 04-04-2004 12:44 AM



Advertisments