KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I have a Python script which needs to calculate the exact size of arbitrary strings displayed in arbitrary fonts in order to generate simple diagrams. I can easily do it with Tkinter. import Tkinter as tk import tkFont root = tk.Tk() canvas = tk.Canvas(root, width=300, height=200) canvas.pack() (x,y) = (5,5) text = "yellow world" fonts = [] for (family,size) in [("times",12),("times",24)]: font = tkFont.Font(family=family, size=size) (w,h) = (font.measure(text),font.metrics("linespace")) print "%s %s: (%s,%s)" % (family,size,w,h) canvas.create_rectangle(x,y,x+w,y+h) canvas.create_text(x,y,text=text,font=font,anchor=tk.NW) fonts.append(font) # save object from garbage collecting y += h+5 tk.mainloop() The results seem to depend on the version of Python and/or the system: Python 2.5 Mac 0S X, times 12: (63,12), times 24: (128,24). Python 2.6 Mac OS X, times 12: (64,14), times 24: (127,27). Python 2.6 Windows XP, times 12: (78,19), times 24: (169,36) http://grab.by/grabs/d24a5035cce0d8032ea4e04cb8c85959.png After Ned Batchelder mentioned it, I discovered that the size of fonts differs from platform to platform. It may not be a deal breaker as long as you stick with Tkinter, which remains consistent with itself. But my complete program does not use Tkinter to perform the actual drawing: it just relies on its font size calculations to generate an output (in SVG or as a Python script to be sent to Nodebox ). And it's there that things go really wrong: Output of mocodo http://grab.by/grabs/f67b951d092dd1f4f490e1469a53bca2.png (Please look at the <a href="http://grab.by/grabs
Tags (comma-separated)
Save Edits
Cancel