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

Discussion in 'Python' started by =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Jul 2, 2003.

  1. 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
    =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=, Jul 2, 2003
    #1
    1. Advertising

  2. =?ISO-8859-1?Q?Mickel_Gr=F6nroos?=

    Eric Brunel Guest

    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 <> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com
    Eric Brunel, Jul 2, 2003
    #2
    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. Eoin Mcloughlin

    Decimal places and huge fractions.

    Eoin Mcloughlin, Feb 11, 2004, in forum: Perl
    Replies:
    5
    Views:
    1,613
    Eoin Mcloughlin
    Feb 13, 2004
  2. Lee Jackson

    XSL-FO...Fractions?

    Lee Jackson, Dec 17, 2004, in forum: XML
    Replies:
    3
    Views:
    693
    David Carlisle
    Dec 17, 2004
  3. Askari
    Replies:
    2
    Views:
    708
    Askari
    Aug 30, 2004
  4. PhilC
    Replies:
    2
    Views:
    884
    PhilC
    Oct 25, 2004
  5. Replies:
    10
    Views:
    260
    Rick Johnson
    Mar 15, 2013
Loading...

Share This Page