473,804 Members | 2,280 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.visibleb ox()

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 3141
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.visibleb ox()

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_rowco nfigure(0, weight=1)
root.grid_colum nconfigure(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=HORIZONT AL, command=cnv.xvi ew)
vs = Scrollbar(root, orient=VERTICAL , command=cnv.yvi ew)
cnv.configure(x scrollcommand=h s.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 showVisibleRegi on():
x1, y1 = cnv.canvasx(0), cnv.canvasy(0)
w, h = cnv.winfo_width (), cnv.winfo_heigh t()
x2, y2 = cnv.canvasx(w), cnv.canvasy(h)
print x1, y1, x2, y2

b = Button(root, text='Show', command=showVis ibleRegion)
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*********@pr agmadev.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
2213
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 entry field of the same size as the rectangle placed exactly over the rectangle thus creating the effect that the rectangle has entered "input mode". When clicking return in the entry field, the value is fed back to a text object within the...
2
1359
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 do the second half. In the snippet "1-2 1/2". I can figure out the part before the dash but not after. The second part is optional for the user. If it is there it must be complete Any hints?
2
7052
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). thanks, Mori
15
14626
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 double, but the program aborts when I input a fraction instead of decimal/integer number. Could someone tell me where the problem is, and what I need to do to correct my code? Thanks a lot in advance, Farah. #include <iostream> using namespace...
11
2094
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 horrizontally b) It does not appear to be totaly square on either of my machines.
6
3585
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 http://www.java2s.com/Code/Python/Event/Usemousetodrawashapeoncanvas.htm . The code itself is: from Tkinter import * trace = 0 class CanvasEventsDemo: def __init__(self, parent=None): canvas = Canvas(width=300, height=300, bg='beige') canvas.pack()
5
10116
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 extends JPanel and I set its size to 210x210.
1
1640
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 textboxes. These can be entered as a whole number and a proper fraction, whole number and an improper fraction, just a proper/inproper fraction, or just a whole number. Form build Requirements: using Microsoft Visual Studio.NET 2003 single form...
4
10101
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 shape goes beyond the right or bottom side. However, I don't get scrollbars when shapes move beyond the left or top side of the canvas. This is bad. I need the behavior similar to Visio where you can drag an object past the left or top and you will...
0
9594
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10599
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10090
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7635
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6863
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5531
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4308
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3832
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.