python统计字符串长度_用Python计算字符串的像素大小

本文探讨了Python脚本中如何准确测量不同字体和平台下字符串的尺寸,发现依赖于Python版本和系统。作者提到Tkinter的不一致性,并寻求替代方案。问题在于不同字体测量的不稳定性,以及像素密度差异。建议使用像素而非点作为单位,并考虑不同平台的像素密度。
摘要由CSDN通过智能技术生成

1586010002-jmsa.png

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:

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:

(Please look at the image in real size. Note that the main font used for these outputs is not Times, but Trebuchet MS.)

I now suspect that such discrepancies can't be avoided with Tkinter. Which other cross-platform solution would you recommend?

解决方案

You have two problems. Let's tackle them one at a time

1: the difference between python 2.5 and 2.6 on the same platform with the same font

These two versions of python use different versions of tk. On my mac box, 2.5 uses tk version 8.4.19 and 2.6 uses 8.5.7. In version 8.5.2 of tk were some changes to the font measurement features of tk. Assuming that the changes were improvements, I think it's safe to assume that the numbers you get from python 2.6 are more accurate than the ones from 2.5.

2: the difference between python 2.6 on the mac and 2.6 on the PC.

Obviously, from the screenshots you include, the PC is using a larger font and thus you get larger numbers for the measurement. The question is, why? You are specifying the font size in points (1/72 of an inch). In order for Tk (or any rendering system) to render the font, it needs to know how many pixels are in an inch on the actual display. This will vary on different systems, and Tk isn't always given an accurate number by the underlying OS in order to do its calculations.

Historically, Apple and Microsoft have standardized on 72ppi and 96ppi regardless of the actual display, so the numbers are always going to be different. For more information about the differences in how the mac and windows calculate pixel density see the Dots Per Inch article on wikipedia.

You might try solving this by specifying a font in pixels rather than in points. You can do this by using negative numbers for the font size.

Finally, one thing you might add to your little example code is to print out the result of the font.actual() command -- you might see something different between your windows and mac boxes, which would explain the differences there. This tells you exactly which font is being used by Tk.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值