为什么在Tkinter中计算的字符串宽度和高度(以像素为单位)在平台之间有所不同? [英] Why does the calculated width and height in pixel of a string in Tkinter differ between platforms?

查看:579
本文介绍了为什么在Tkinter中计算的字符串宽度和高度(以像素为单位)在平台之间有所不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python脚本,该脚本需要计算以任意字体显示的任意字符串的确切大小,以便生成简单的图表。我可以轻松地用Tkinter做到这一点。

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()

结果似乎取决于Python和/或系统的版本:

The results seem to depend on the version of Python and/or the system:

Python 2.5 Mac 0S X,时间12:(63,12),时间24 :(128,24)。 Python 2.6 Mac OS X,时间12:(64,14),时间24:(127,27)。 Python 2.6 Windows XP,时间12:(78,19),时间24:(169,36)http://grab.by/grabs/d24a5035cce0d8032ea4e04cb8c85959.png

在内德·巴切尔德(Ned Batchelder)提到它之后,我发现字体因平台而异。只要您坚持使用Tkinter,这可能不会破坏交易。但是我的完整程序并没有使用Tkinter进行实际绘制:它仅依靠其字体大小计算来生成输出(以SVG形式或以Python脚本的形式发送到 Nodebox )。那里真的出错了:

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:

输出的Mocodo http://grab.by/grabs/f67b951d092dd1f4f490e1469a53bca2.png

(请查看以实际大小显示的图像。请注意,这些输出所使用的主要字体不是Times,而是Trebuchet MS。)

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

我现在怀疑使用Tkinter无法避免这种差异。您将推荐哪种其他跨平台解决方案?

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:在同一平台上使用相同字体的python 2.5和2.6之间的区别

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

这两个python版本使用不同版本的tk。在我的Mac电脑上,2.5使用tk版本8.4.19,而2.6使用8.5.7。在tk的8.5.2版中,对tk的字体测量功能进行了一些更改。假定这些更改是改进,我认为可以肯定地认为从python 2.6获得的数字比从2.5获得的数字更准确。

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:在Mac上为python 2.6,在PC上为2.6。

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

很明显,从您包括的屏幕截图中,PC使用的是较大的字体,因此,您获得的数字更大。问题是,为什么?您以磅(1/72英寸)为单位指定字体大小。为了让Tk(或任何渲染系统)渲染字体,它需要知道实际显示器上一英寸的像素数。这在不同的系统上会有所不同,并且底层操作系统不一定总是为Tk提供准确的数字来进行计算。

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.

从历史上看,苹果和微软已经将72ppi和96ppi标准化,而不管实际显示的是什么,因此数字总是会有所不同。有关Mac和Windows在计算像素密度方面的差异的更多信息,请参见每英寸点数维基百科上的文章。

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.

最后,您可能会添加到小示例代码中的一件事是打印出<$的结果。 c $ c> font.actual()命令-您可能会在Windows和Mac框之间看到一些不同之处,这将解释那里的区别。这可以准确告诉您Tk正在使用哪种字体。

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.

这篇关于为什么在Tkinter中计算的字符串宽度和高度(以像素为单位)在平台之间有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆