随着字符串的增长,字体中的Python Tkinter字符串的度量方式与Text widgit中的度量方式不同 [英] Python Tkinter string in a font measures differently than same in Text widgit as string grows

查看:120
本文介绍了随着字符串的增长,字体中的Python Tkinter字符串的度量方式与Text widgit中的度量方式不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

包含字符串(以指定字体)的Text对象似乎根据字符串的长度给出不一致的结果.例如:

The Text Object holding a string (in a specified font) seems to give inconsistent results depending on the length of the string. For example:

from Tkinter import *
import tkFont
root=Tk()
t_Font = tkFont.Font(family='Helvetica', size=12, weight='bold')
t_text='New.'
t_frame = Frame(root, bd=0, height=10, width=t_Font.measure(t_text))
t = Text(master=t_frame, height=1, width=len(t_text), bd=1, font=t_Font, padx=0 )
print '\n\nMeasured:',t_Font.measure(t_text),'Frame req:',t_frame.winfo_reqwidth(),'As Text:',t.winfo_reqwidth()

测量值:38帧要求:38作为文本:38

Measured: 38 Frame req: 38 As Text: 38

t_text='New title.'
t_frame = Frame(root, bd=0, height=10, width=t_Font.measure(t_text))
t = Text(master=t_frame, height=1, width=len(t_text), bd=1, font=t_Font, padx=0 )
print '\n\nMeasured:',t_Font.measure(t_text),'Frame req:',t_frame.winfo_reqwidth(),'As Text:',t.winfo_reqwidth()

已测量:69个帧要求:69个文本:92

Measured: 69 Frame req: 69 As Text: 92

另外6个字符将测量的大小和帧大小增加了31个像素,但是Text对象增加了54个.

The additional 6 characters increased the measured size and frame size by 31 pixels, but the Text object has increased by 54.

这是什么原因?

推荐答案

我意识到已经7个月了,但是想为像我一样在这里结束的任何人回答这个问题.

I realize its been 7 months, but wanted to answer this for anyone that ends up here like I did.

简短的回答:如果您使用的是固定宽度的字体,那么这将是一个匹配项(例如"Courier New").但是Helvetica是一种比例字体,因此其字符宽度并不相同.

Short answer: if you were using a fixed-width font, then it would have been a match (e.g. "Courier New"). But Helvetica is a proportional font, so its characters are not all the same width.

Font.measure()和Frame.winfo_reqwidth()都使用该字体/粗细/大小的文本字符串的实际大小,因为它们的宽度以像素为单位.

The Font.measure() and Frame.winfo_reqwidth() are both using the actual size for those strings of text in that font/weight/size, because their widths are specified in pixels.

文本"小部件的宽度由字符指定.

The Text widget on the other hand has its width specified in characters.

因此,它每次都采用字符数,并尝试猜测该字体/粗细/大小使小部件能够处理它们的大小-但不是您所使用的确切字符.它使用零字符"0"作为其平均字符大小.

So its taking the number of characters each time and trying to guess for that font/weight/size how big to make the widget to handle them - but not the exact characters you're using. It uses the zero character, "0", as its average character size.

如果将第二组t_text,t_frame,t更改为t_text2,t_frame2,t2,然后打包()所有内容并启动root.mainloop(),则可以使用创建的2个小部件.第一个带有"New".键入甚至不显示."因为创建的字段太小,而第二个窗口小部件显示新标题".剩下多余的空间.现在,如果删除它们,并在第一个窗口小部件中输入"0000",然后在第二个窗口中输入"0000000000",则将看到窗口小部件完全填满.

If you change your second set of t_text, t_frame, t to t_text2, t_frame2, t2, and then pack() everything and start root.mainloop(), you can play around with the 2 widgets created. The first one with "New." typed in doesn't even show the "." because the created field is slightly too small, while the second widget shows "New title." with extra spaces left. Now if you delete those and enter "0000" for the first widget, and "0000000000" for the second, you'll see the widgets filled up exactly.

我通过阅读Tcl/Tk文档中的

I found this through reading the Tcl/Tk docs for text -width at https://www.tcl.tk/man/tcl8.6/TkCmd/text.htm#M21

这篇关于随着字符串的增长,字体中的Python Tkinter字符串的度量方式与Text widgit中的度量方式不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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