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

Discussion in 'Python' started by PhilC, Oct 23, 2004.

  1. PhilC

    PhilC Guest

    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
     
    PhilC, Oct 23, 2004
    #1
    1. Advertising

  2. PhilC

    Eric Brunel Guest

    Re: Canvas with scrollbars - how to get correct canvas coordinatewhen the scroll bars have moved?

    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
     
    Eric Brunel, Oct 25, 2004
    #2
    1. Advertising

  3. PhilC

    PhilC Guest

    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
    <> 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
     
    PhilC, Oct 25, 2004
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. S. Justin Gengo

    Re: DIVs and overflow scroll bars

    S. Justin Gengo, Jul 18, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    418
    S. Justin Gengo
    Jul 18, 2003
  2. UJ
    Replies:
    1
    Views:
    583
    John Timney \(MVP\)
    Nov 1, 2006
  3. active

    Can't get textbox to display scroll bars

    active, Apr 7, 2007, in forum: ASP .Net Web Controls
    Replies:
    4
    Views:
    173
    active
    Apr 7, 2007
  4. JCO
    Replies:
    0
    Views:
    115
  5. oyster
    Replies:
    0
    Views:
    260
    oyster
    Jun 25, 2012
Loading...

Share This Page