Python Tkinter标签高度适合内容
我有一个标签,我要把不同大小的内容放进去。我想知道我需要多高的标签,以便我可以大小的窗口,使它可以保持不同的内容大小相同。我有一个策略,但它似乎比应该的更复杂 我想将标签设置为给定的宽度和宽度:Python Tkinter标签高度适合内容,python,tkinter,Python,Tkinter,我有一个标签,我要把不同大小的内容放进去。我想知道我需要多高的标签,以便我可以大小的窗口,使它可以保持不同的内容大小相同。我有一个策略,但它似乎比应该的更复杂 我想将标签设置为给定的宽度和宽度: l = Label(root) l['width'] = 30 l['wraplength'] = 244 l['text'] = "testing this" 现在我想查询标签,以确定使用了多少行。l['height']保持为0,因此我能想到的最好方法是使用l.winfo_height()并将以像素
l = Label(root)
l['width'] = 30
l['wraplength'] = 244
l['text'] = "testing this"
现在我想查询标签,以确定使用了多少行。l['height']保持为0,因此我能想到的最好方法是使用l.winfo_height()并将以像素为单位的高度转换为使用的行数。dir(l)中的任何内容似乎都不能直接向我提供信息,但这种策略很容易受到字体更改和其他更改的影响
有什么建议吗
更新:使用Brian Oakley的建议(类似于我在usenet上得到的建议),我对解决方案有以下近似值(需要抛光,例如,不考虑标签在空白处断裂):
将Tkinter作为Tk导入
导入tkFont
随机输入
导入系统
def genstr(j):
rno=random.randint(4,50)
ret_val=str(j)+”
对于范围内的i(0,rno):
ret_val+=“你好”+str(一)
返回返回值
def gendata(左侧):
ret_val=[]
对于范围内的i(0,左侧):
检索附加值(genstr(i))
返回返回值
数据=性别数据(100)
root=Tk.Tk()
font=tkFont.font(family='times',size=13)
类别行:
定义初始化(自):
self.lastct=1#还记得截止日期是从那里开始的最后一次工作吗
def计数(自身、文本、截止=400):
全局字体
无线=1
start_idx=0
idx=self.lastct
尽管如此:
如果idx>len(文本):
idx=len(文本)
#缩小猜测值
而font.measure(text[start\u idx:idx-1])>截止:
如果idx正确,则高度属性不会更改。该属性不会告诉您实际高度,只会告诉您它配置为的高度。实际高度取决于一些因素,如文本的数量、包裹长度、字体以及小部件几何体的管理方式
tkinter字体对象有一个measure
方法,可用于确定给定字体的字符串高度和宽度。您可以获取小部件的字体,并使用该方法确定字符串需要多少空间
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()