简体   繁体   English

从 tkinter Text 小部件中删除尾随空格

[英]Remove trailing space from tkinter Text widget

My goal here is to have the right edge of the text box fit to the end of the text.我的目标是让文本框的右边缘适合文本的末尾。 This happens automatically when using TkFixedFont , but when using TkDefaultFont (which is what I would like to use), there is trailing whitespace added to the end.这在使用TkFixedFont时会自动发生,但在使用TkDefaultFont (这是我想使用的)时,末尾添加了尾随空格。 This is what it looks like with no trailing space, with TkFixedFont :这是没有尾随空格的样子,使用TkFixedFont

Notice that the trailing space is minimal.请注意,尾随空间很小。 I would like this same near-none amount of trailing space, but when using TkDefaultFont .我想要同样的几乎没有尾随空间,但是在使用TkDefaultFont


The following is a minimal example of my code:以下是我的代码的最小示例:

from tkinter import *
root = Tk()

myText = 'sample text'

text = Text(root, bg='#c2d9ff', 
            font='TkDefaultFont',
            height=1, width=len(myText)
        )
text.insert(INSERT, myText)
text.place(x=0,y=0)
root.mainloop()

I have searched for around two hours now for everything related to spacing, trailing space, types of widgets (eg, label vs text, etc.), widget options, etc., and have found nothing unfortunately.我已经搜索了大约两个小时,以查找与间距、尾随空格、小部件类型(例如,标签与文本等)、小部件选项等相关的所有内容,但不幸的是,一无所获。

It appears to somehow be proportional to the length of the text, which I cannot figure out.它似乎与文本的长度成正比,我无法弄清楚。 Using the code above, when myText = 'Some text goes here' , the trailing spacing is:使用上面的代码,当myText = 'Some text goes here' ,尾随间距为:

But when I put a longer text string, the trailing space seems to proportionally increase:但是当我放置更长的文本字符串时,尾随空间似乎成比例地增加:

Any suggestions on how to cut out the trailing space would be much appreciated!任何关于如何削减尾随空间的建议将不胜感激!

What you're seeing isn't whitespace characters, it's just empty space.你看到的不是空白字符,它只是空白。 The actual width is the given number of characters times the number of pixels in an average-sized character (I think tkinter uses the character "0").实际宽度是给定的字符数乘以平均大小字符中的像素数(我认为 tkinter 使用字符“0”)。 In a variable-width font, "."在可变宽度字体中,“.” is narrower than, say "W", so the actual width of a string depends on what characters are in the string.比“W”窄,因此字符串的实际宽度取决于字符串中的字符。

In other words, if your text is made up of narrow characters (eg: "...........") there will be more empty space, if it's made up of wide characters (eg: "WWWWWWWWWWW") there will be less.换句话说,如果您的文本由窄字符组成(例如:“...........”),如果由宽字符组成(例如:“WWWWWWWWWWW”),则会有更多的空白空间) 会更少。

What you can do is compute the actual width of the text using the measure method of a font object.您可以做的是使用字体对象的measure方法计算文本的实际宽度。 Once you know exactly how many pixels your text string requires, you can use that information to set the size of your text widget appropriately.一旦您确切地知道您的文本字符串需要多少像素,您就可以使用该信息来适当地设置文本小部件的大小。

from tkinter import *
from tkinter.font import nametofont

root = Tk()

myText = 'sample text'
font = nametofont("TkDefaultFont")
width = font.measure(myText)

text = Text(root, bg='#c2d9ff',
            font='TkDefaultFont',
            height=1, width=len(myText)
        )
text.insert(INSERT, myText)
text.place(x=0,y=0, width=width)
root.mainloop()

Note that when you use the width with place , that specifies the entire width of the widget, not just the width inside the border.请注意,当您将宽度与place一起使用时,它指定了小部件的整个宽度,而不仅仅是边框内的宽度。 You'll need to adjust the width to account for the text widget border and highlight thickness, and maybe a few other pixels for the interior padding.您需要调整宽度以考虑文本小部件边框和突出显示的厚度,以及内部填充的其他一些像素。

For example, if the text itself takes up 100 pixels and you have a two pixel border and a one pixel highlight ring for the text widget, you'll need to add at least six pixels to the width when used in the call to place .例如,如果文本本身占用 100 像素,并且文本小部件有一个 2 像素的边框和一个 1 像素的高亮环,则在调用place时,您需要在宽度上至少添加 6 个像素。

You can get the area occupied by myText using Text.dlineinfo() and adjust the width of text based on the result and other settings related to space occupied horizontally, eg border width, padx, etc:您可以使用Text.dlineinfo()获取myText占用的区域,并根据结果和与水平占用空间相关的其他设置(例如边框宽度、padx 等Text.dlineinfo()调整text的宽度:

myText = 'sample text'*4

text = Text(root, bg='#c2d9ff',
            font='TkDefaultFont',
            height=1
        )
text.insert(INSERT, myText)
text.place(x=0, y=0)

text.update_idletasks() # make sure the widget is laid out completely
x, y, w, h, _ = text.dlineinfo(INSERT)
text.place(width=x+w+(text['bd']+text['padx'])*2) # adjust the width

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM