473,396 Members | 1,971 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,396 software developers and data experts.

How do I get the fractions of the visible part of a canvas?

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 from the left the
left visibility border is and from what fraction to the right the right
visibility border is.

Consider this ascii picture as an example

+-------------------------------+
| |<-- the full canvas
| a------------------+ |
| | |<--------- the currently visible part
| +------------------b |
| |
+-------------------------------+

I would like to be able to ask the canvas something like:

t = canvas.visiblebox()

and it would return a two-tuple of two-tuples with coordinates (of the
a and b points in the picture above), say:

t = ((10,10), (90,30))

Using these values I could calculate the fractions myself.

Any ideas?

/Mickel

--
Mickel Grönroos, application specialist, linguistics, Research support,CSC
PL 405 (Tekniikantie 15 a D), 02101 Espoo, Finland, phone +358-9-4572237
CSC is the Finnish IT center for science, www.csc.fi
Jul 18 '05 #1
1 3117
Mickel Grönroos wrote:
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 from the left the
left visibility border is and from what fraction to the right the right
visibility border is.

Consider this ascii picture as an example

+-------------------------------+
| |<-- the full canvas
| a------------------+ |
| | |<--------- the currently visible part
| +------------------b |
| |
+-------------------------------+

I would like to be able to ask the canvas something like:

t = canvas.visiblebox()

and it would return a two-tuple of two-tuples with coordinates (of the
a and b points in the picture above), say:

t = ((10,10), (90,30))

Using these values I could calculate the fractions myself.

Any ideas?


This should do what you want:

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

## Initialize Tk
root = Tk()
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)

## Create the canvas
cnv = Canvas(root, scrollregion=(0, 0, 1000, 1000), width=200, height=200)
cnv.grid(row=0, column=0, sticky='nswe')

## Create the scrollbars
hs = Scrollbar(root, orient=HORIZONTAL, command=cnv.xview)
vs = Scrollbar(root, orient=VERTICAL, command=cnv.yview)
cnv.configure(xscrollcommand=hs.set, yscrollcommand=vs.set)
hs.grid(row=1, column=0, sticky='we')
vs.grid(row=0, column=1, sticky='ns')

## This is the function you want:
def showVisibleRegion():
x1, y1 = cnv.canvasx(0), cnv.canvasy(0)
w, h = cnv.winfo_width(), cnv.winfo_height()
x2, y2 = cnv.canvasx(w), cnv.canvasy(h)
print x1, y1, x2, y2

b = Button(root, text='Show', command=showVisibleRegion)
b.grid(row=2, column=0, columnspan=2)

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

The methods canvasx and canvasy on a Canvas convert a coordinate in the
displayed canvas to a coordinate in the underlying region:

+------------------------------+
| |
| |
| +----------------------+ |
| | | |
| |<--x-->| | |
| | + | |
| | | | |
| +-------|--------------+ |
|<---xx---->| |
| |
+------------------------------+

Here, cnv.canvasx(x) = xx

So, taking the canvasx and canvasy of (0, 0) gives you the coordinates for the
top-left corner of the region you want, and taking the canvasx and canvasy of
the canvas's dimensions gives you the bottom-right one.

HTH
--
- Eric Brunel <er*********@pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #2

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

Similar topics

0
by: Mickel Grönroos | last post by:
Hi, I'm trying to put an Tkinter.Entry of fixed size onto a specific location on a canvas using the place manager. The idea is that one can double-click a rectangle object on a canvas to get an...
2
by: Stedak | last post by:
C# I am writing a function that allows the user to enter fractions as answers (i.e. how many asprin tablets for a dosage?). I have the first half of the Regex figured out but don't know how to...
2
by: Mori | last post by:
Hi, Can someone supply a code example of displaying a string with a fractional part, say 5 and 7 16ths. I cannot find an example of how to use the Encoding object (if that is what you use). ...
15
by: farah727rash | last post by:
Hi everyone, I have this problem and I don't know what's wrong with my program. I am trying to enter my two variables height and weight as fraction numbers. I declared them as float and also as...
11
by: Aaron Gray | last post by:
Hi, I have put together a bit of JavaScript to make a square resizable canvas :- http://www.aarongray.org/Test/JavaScript/resizable.html Problems I have :- a) I cannot seem to center it...
6
by: Nebulism | last post by:
I have been attempting to utilize a draw command script that loads a canvas, and through certain mouse events, draws rectangles. The original code is from...
5
by: James Barrett | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi all, I am experimenting with JApplet and JPanel. My JApplet contains a JPanel called jpanel1 size 210x210. I created a class myPanel which...
1
by: Semajthewise | last post by:
Here it is cleaned up a little more. Here's what this code does. It will take 2 fractions and add, subtract, multiply, or divide them. The user enters the fractions to be calculated into two...
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
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
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.