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

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
Jul 18 '05 #1
3 2114
John Velman wrote:
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
--
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com

Jul 18 '05 #2
On Tue, 28 Sep 2004 10:26:01 +0200, Eric Brunel
<er*********@despammed.com> declaimed the following in comp.lang.python:
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.

-- ================================================== ============ <
wl*****@ix.netcom.com | Wulfraed Dennis Lee Bieber KD6MOG <
wu******@dm.net | Bestiaria Support Staff <
================================================== ============ <
Home Page: <http://www.dm.net/~wulfraed/> <
Overflow Page: <http://wlfraed.home.netcom.com/> <

Jul 18 '05 #3
On Tue, 28 Sep 2004 10:26:01 +0200, Eric Brunel wrote:
John Velman wrote:
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?


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]
Jul 18 '05 #4

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

Similar topics

1
by: Josh | last post by:
Caution, newbie approaching... I'm trying to come up with a very simple Tkinter test application that consists of a window with a drop-down menu bar at the top and a grid of colored rectangles...
3
by: srijit | last post by:
Hello, Any idea - why the following code crashes on my Win 98 machine with Python 2.3? Everytime I run this code, I have to reboot my machine. I also have Win32all-157 installed. from Tkinter...
1
by: syed_saqib_ali | last post by:
Please take a look at and run the code snippet shown below. It creates a canvas with vertical & Horizontal scroll-bars. If you shrink the window to smaller than the area of the canvas, the...
0
by: syed_saqib_ali | last post by:
Below is a simple code snippet showing a Tkinter Window bearing a canvas and 2 connected scrollbars (Vertical & Horizontal). Works fine. When you shrink/resize the window the scrollbars adjust...
1
by: Michael Yanowitz | last post by:
Hello: Below I have included a stripped down version of the GUI I am working on. It contains 2 dialog boxes - one main and one settings. It has the following problems, probably all related, that...
3
by: dwelch91 | last post by:
I'm trying unsuccessfully to do something in Tk that I though would be easy. After Googling this all day, I think I need some help. I am admittedly very novice with Tk (I started with it...
1
by: yvesd | last post by:
hello i want to intercept tkinter python system events like wm_delete_window and if possible for any window, but the newest code I've produced give me an error : Traceback (most recent call...
8
by: karthikbalaguru | last post by:
Hi, One of my python program needs tkinter to be installed to run successfully. I am using Redhat 9.0 and hence tried installing by copying the tkinter-2.2.2-36.i386.rpm alone from the CD 3 to...
3
by: joshdw4 | last post by:
I hate to do this, but I've thoroughly exhausted google search. Yes, it's that pesky root window and I have tried withdraw to no avail. I'm assuming this is because of the methods I'm using. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.