473,404 Members | 2,195 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

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

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
Jul 18 '05 #1
2 6083
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
Jul 18 '05 #2
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
<er*********@despammed.com> 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


Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mickel Grönroos | last post by:
Hi, I have a Tkinter.Canvas of variable width. Is there a standard way of asking the canvas which parts of it that is visible? I.e. on the horizontal scale, I would like to know at what fraction...
0
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
24
by: Nobody | last post by:
Okay, you are all so smart in here. Answer me this: IE6 in standards mode doesn't seem to hide scrollbars on the body element (overflow:hide) Ain't this a quandary. I have it in my head that I...
17
by: Dino M. Buljubasic | last post by:
I have a treeview and a checked list view controls one beside another. I want to make them work so that when I scroll down or up one of them the other does the same. Any help will be...
1
by: dbuchanan | last post by:
Hello, My dataGridView is configured with the "ScrollBars" set to both. When the form is displayed no scroll bars appear. Therefore I don't have access to the columns beyond the right side of...
1
by: Bob Greschke | last post by:
I have a program that sucks in a list of equipment positions (Lats/Longs), opens a Toplevel frame with a canvas set to, for example, 700x480 pixels, and then does all of the calculations and plots...
2
by: MrNobody | last post by:
I have been struggling with .NET's scrollbars because when I use them I can't seem to get them to go to their maximum value. Searching on the web it looks like the maximum Value you can get on...
1
by: Shankartalukdar | last post by:
Dear all, I am currently drawing a visualization tool in python using Tkinter. I would like to know how to add scroll bars into the canvas widget using the Tkinter module.Could you...
4
by: moondaddy | last post by:
I have a wpf project where I use a canvas to drag shapes around. when I drag a shape beyond the right or bottom side I get scrollbars which is good. I also get scrollbars when I zoom in and a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.