我有一个标签,我将在其中放入不同大小的内容。我想知道我需要将标签制作多高,以便我可以调整窗口大小,以便它可以针对不同的内容大小保持不变。我有一个策略,但它似乎比应有的更复杂。
我想将标签设置为给定的宽度和环绕长度:
l = Label(root)
l['width'] = 30
l['wraplength'] = 244
l['text'] = "testing this"
现在我想查询标签以查找使用了多少行。 l['height'] 保持在 0,所以我能想到的最好方法是使用 l.winfo_height() 并将以像素为单位的高度转换为所使用的行数。 dir(l) 中的任何内容似乎都没有直接向我提供信息,但这种策略对于字体更改和其他更改很脆弱。
有什么建议吗?
更新:使用 Brian Oakley 的建议(类似于我在 usenet 上得到的建议),我有以下近似的解决方案(需要改进,例如,不考虑标签在空白处中断):
import Tkinter as Tk
import tkFont
import random
import sys
def genstr (j):
rno = random.randint(4,50)
ret_val = str(j) + ":"
for i in range (0, rno):
ret_val += "hello" + str(i)
return ret_val
def gendata (lh):
ret_val = []
for i in range(0,lh):
ret_val.append (genstr (i))
return ret_val
data = gendata (100)
root = Tk.Tk()
font = tkFont.Font(family='times', size=13)
class lines:
def __init__ (self):
self.lastct = 1 # remember where the cutoff was last work from there
def count (self, text, cutoff = 400):
global font
no_lines = 1
start_idx = 0
idx = self.lastct
while True:
if idx > len (text):
idx = len (text)
# shrink from guessed value
while font.measure (text[start_idx:idx - 1]) > cutoff:
if idx <= start_idx:
print "error"
sys.exit ()
else:
idx -= 1
self.lastct = idx - start_idx # adjust since was too big
# increase from guessed value (note: if first shrunk then done)
while (idx < len (text)
and font.measure (text[start_idx:idx]) < cutoff):
idx += 1
self.lastct = idx - start_idx # adjust since was too small
# next line has been determined
print "*" + text[start_idx:idx-1] + "*"
if idx == len(text) and font.measure (text[start_idx:]) < cutoff:
return no_lines
elif idx == len(text):
return no_lines + 1
else:
no_lines += 1
start_idx = idx - 1
idx = start_idx + self.lastct
lin = lines()
for i in range(0,len(data)):
lin.count(data[i], 450)
for i in range(0,min(len(data),10)):
l = Tk.Label(root)
l.pack()
l['text'] = data[i]
print i
no = lin.count (data[i], 450)
print "computed lines", no
l['width'] = 50
l['justify'] = Tk.LEFT
l['anchor'] = 'w'
l['wraplength'] = 450
l['padx']=10
l['pady'] = 5
l['height'] = no
l['font'] = font
if i % 2 == 0:
l['background'] = 'grey80'
else:
l['background'] = 'grey70'
root.mainloop()
最佳答案
您的说法是正确的,height
属性不会改变。该属性不会告诉您实际的高度,只会告诉您其配置的高度。实际高度取决于其中有多少文本、换行长度、字体以及小部件几何形状的管理方式等因素。
tkinter Font 对象有一个 measure
方法,可让您确定给定字体的字符串的高度和宽度。您可以获取小部件的字体,并使用该方法来确定字符串需要多少空间。
关于python - Tkinter 标签高度适合内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7294852/