Tkinter measurements

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John Velman

    Tkinter measurements

    I want to draw a box around a short piece of text in canvas (one line
    text). I know how to do it if I place the text on the canvas first,then
    draw the box around it.

    Is there a way to find out the dimensions of the text bounding box before
    drawing it?

    Also, is there a method for converting from pixels to inches or inches to
    pixels (for canvas)?


    Thanks,

    John Velman
  • Eric Brunel

    #2
    Re: Tkinter measurements

    John Velman wrote:[color=blue]
    > I want to draw a box around a short piece of text in canvas (one line
    > text). I know how to do it if I place the text on the canvas first,then
    > draw the box around it.
    >
    > Is there a way to find out the dimensions of the text bounding box before
    > drawing it?[/color]

    First: why do you want to do that? The most used method is to draw the text
    before, get its bounding box, then draw the box. Why do you want to do the opposite?

    Anyway, there are some ways to get the size of the displayed text via the tkFont
    module. Example:

    --------------------------------------------------------------
    from Tkinter import *
    from tkFont import Font

    root = Tk()
    cnv = Canvas(root)
    cnv.pack()

    ## This text item is only used to get the default font in the canvas
    t = cnv.create_text (0, 0, text='')
    f = Font(font=cnv.i temcget(t, 'font'))

    s = 'spam, spam and spam'
    cnv.create_text (10, 10, text=s, anchor=W)
    cnv.create_line (10, 15, 10 + f.measure(s), 15)

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

    So you can get the text width quite easily. For the text height, it only depends
    on the font size and the number of lines in the text, so computing it "manually"
    seems reasonable.
    [color=blue]
    > Also, is there a method for converting from pixels to inches or inches to
    > pixels (for canvas)?[/color]

    The solution I use is just to multiply or divide by 72. Be careful with that: if
    you use it on font sizes, some windowing systems try to be smart and consider
    the screen resolution when displaying text. To avoid this, you can use negative
    font sizes (e.g. myText.configur e(font=('helvet ica', -12))). A negative font
    size is always considered in "regular" pixels (one inch / 72), and does not
    depend on the screen resolution.

    HTH
    --
    - Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
    PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

    Comment

    • Dennis Lee Bieber

      #3
      Re: Tkinter measurements

      On Tue, 28 Sep 2004 10:26:01 +0200, Eric Brunel
      <eric_brunel@de spammed.com> declaimed the following in comp.lang.pytho n:
      [color=blue]
      > the screen resolution when displaying text. To avoid this, you can use negative
      > font sizes (e.g. myText.configur e(font=('helvet ica', -12))). A negative font
      > size is always considered in "regular" pixels (one inch / 72), and does not
      > depend on the screen resolution.
      >[/color]
      To be clear, that '"regular" pixels' should probably be referred
      to a "points"... Standard typography runs 72 points per inch (well,
      since the Mac, at least... Prior to the computerized revolution, I
      understand it was 72.27 points per inch).

      That is where the Mac's desktop publishing WYSIWYG came about --
      regardless of monitor size, the original Mac's were always 72dpi
      resolution (none of the Windows running 640x480 on a 20" monitor <G>).
      That meant whatever was shown on screen was shown at the same size as
      the print version

      The DTP program on my former Amiga incorporated its own scaling
      factors (for both x and y), allowing one to calibrate for non-square
      pixel resolutions.

      --[color=blue]
      > =============== =============== =============== =============== == <
      > wlfraed@ix.netc om.com | Wulfraed Dennis Lee Bieber KD6MOG <
      > wulfraed@dm.net | Bestiaria Support Staff <
      > =============== =============== =============== =============== == <
      > Home Page: <http://www.dm.net/~wulfraed/> <
      > Overflow Page: <http://wlfraed.home.ne tcom.com/> <[/color]

      Comment

      • John Velman

        #4
        Re: Tkinter measurements

        On Tue, 28 Sep 2004 10:26:01 +0200, Eric Brunel wrote:
        [color=blue]
        > John Velman wrote:[color=green]
        >> I want to draw a box around a short piece of text in canvas (one line
        >> text). I know how to do it if I place the text on the canvas first,then
        >> draw the box around it.
        >>
        >> Is there a way to find out the dimensions of the text bounding box before
        >> drawing it?[/color][/color]
        [color=blue]
        >
        > First: why do you want to do that? The most used method is to draw the
        > text before, get its bounding box, then draw the box. Why do you want to
        > do the opposite?
        >[/color]

        That's a good question, all right. I'm restarting in Python something I'd
        originally started in perl with Perl/TK. In my perl original my code was
        pretty unstructured, and in redoing I decided to modularize (which Python
        makes a lot easier!). Without going into my whole possibly misbegotten
        concept, my original idea for modules seemed to require knowing the text
        size. Probably a better design will let me do the text, then the box as
        you suggest. Sometimes I get hung up on a certain problem (in this case,
        getting the text size before drawing it) and can't seem to move on until I
        have a solution.

        Thanks for the good information about TkFont module, and the point sized
        pseudo pixels!

        And for the question "why?"

        Best,

        John Velman

        [snip]

        Comment

        Working...