Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Canvas with scrollbars - how to get correct canvas coordinate when the scroll bars have moved?

Reply
Thread Tools

Canvas with scrollbars - how to get correct canvas coordinate when the scroll bars have moved?

 
 
PhilC
Guest
Posts: n/a
 
      10-23-2004
Hi Folks,

I'm trying to click on a canvas to draw a line. The canvas has scroll
bars. All is well until I move the scrollbars. This naturally because
the relationship between my screen click and canvas event.x, event.y
has changed.

I need something that will add the proportional amount the scroll bar
has moved to the mouse click position. I see the terms canvasx and
canvasy but they appear to be used for converting screen position to
canvas position.

A pointer in the right direction would be appreciated.

Thanks

PhilC
 
Reply With Quote
 
 
 
 
Eric Brunel
Guest
Posts: n/a
 
      10-25-2004
PhilC wrote:
> Hi Folks,
>
> I'm trying to click on a canvas to draw a line. The canvas has scroll
> bars. All is well until I move the scrollbars. This naturally because
> the relationship between my screen click and canvas event.x, event.y
> has changed.
>
> I need something that will add the proportional amount the scroll bar
> has moved to the mouse click position. I see the terms canvasx and
> canvasy but they appear to be used for converting screen position to
> canvas position.


Either I do not understand your question, or canvasx and canvasy are exactly
what you're looking for:

------------------------------------------------------------
from Tkinter import *

root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
cnv = Canvas(root, scrollregion=(0, 0, 1000, 1000))
cnv.grid(row=0, column=0, sticky='nswe')
hs = Scrollbar(root, orient=HORIZONTAL, command=cnv.xview)
hs.grid(row=1, column=0, sticky='we')
vs = Scrollbar(root, orient=VERTICAL, command=cnv.yview)
vs.grid(row=0, column=1, sticky='ns')
cnv.configure(xscrollcommand=hs.set, yscrollcommand=vs.set)

def click(evt):
x, y = cnv.canvasx(evt.x), cnv.canvasy(evt.y)
cnv.create_oval(x - 5, y - 5, x + 5, y + 5)

cnv.bind('<1>', click)

root.mainloop()
------------------------------------------------------------

"Canvas position" is the position in the drawable zone for the canvas,
considering the scroll-region. Isn't it what you want?

HTH
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
 
Reply With Quote
 
 
 
 
PhilC
Guest
Posts: n/a
 
      10-25-2004
Thanks Eric,

I thought so too

Re looked at my script and I had an error further down. Corrected that
and the scrolling all works. Part of my inexperience is not knowing
where to look for the errors. I'm sure that comes with practice.

Many thanks for taking the time to answer me it is much appreciated.

Regards

Phil

On Mon, 25 Oct 2004 10:16:09 +0200, Eric Brunel
<(E-Mail Removed)> wrote:

>PhilC wrote:
>> Hi Folks,
>>
>> I'm trying to click on a canvas to draw a line. The canvas has scroll
>> bars. All is well until I move the scrollbars. This naturally because
>> the relationship between my screen click and canvas event.x, event.y
>> has changed.
>>
>> I need something that will add the proportional amount the scroll bar
>> has moved to the mouse click position. I see the terms canvasx and
>> canvasy but they appear to be used for converting screen position to
>> canvas position.

>
>Either I do not understand your question, or canvasx and canvasy are exactly
>what you're looking for:
>
>------------------------------------------------------------
>from Tkinter import *
>
>root = Tk()
>root.grid_rowconfigure(0, weight=1)
>root.grid_columnconfigure(0, weight=1)
>cnv = Canvas(root, scrollregion=(0, 0, 1000, 1000))
>cnv.grid(row=0, column=0, sticky='nswe')
>hs = Scrollbar(root, orient=HORIZONTAL, command=cnv.xview)
>hs.grid(row=1, column=0, sticky='we')
>vs = Scrollbar(root, orient=VERTICAL, command=cnv.yview)
>vs.grid(row=0, column=1, sticky='ns')
>cnv.configure(xscrollcommand=hs.set, yscrollcommand=vs.set)
>
>def click(evt):
> x, y = cnv.canvasx(evt.x), cnv.canvasy(evt.y)
> cnv.create_oval(x - 5, y - 5, x + 5, y + 5)
>
>cnv.bind('<1>', click)
>
>root.mainloop()
>------------------------------------------------------------
>
>"Canvas position" is the position in the drawable zone for the canvas,
>considering the scroll-region. Isn't it what you want?
>
>HTH


 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
image lib supports arbitrary canvas coordinate oyster Python 0 06-25-2012 02:16 AM
Can't get textbox to display scroll bars active ASP .Net Web Controls 4 04-07-2007 08:03 PM
Using a div with scroll bars - can we get the scroll bars on the left instead of the right side? UJ ASP .Net 1 11-01-2006 09:32 PM
Can't get FrameSets to Display Scroll Bars JCO Javascript 0 03-01-2004 08:36 PM



Advertisments