Tkinter measurements

J

John Velman

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
 
E

Eric Brunel

John said:
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?

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.itemcget(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.
Also, is there a method for converting from pixels to inches or inches to
pixels (for canvas)?

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.configure(font=('helvetica', -12))). A negative font
size is always considered in "regular" pixels (one inch / 72), and does not
depend on the screen resolution.

HTH
 
D

Dennis Lee Bieber

the screen resolution when displaying text. To avoid this, you can use negative
font sizes (e.g. myText.configure(font=('helvetica', -12))). A negative font
size is always considered in "regular" pixels (one inch / 72), and does not
depend on the screen resolution.
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.

--
 
J

John Velman

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?

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]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top