使用tkinter将文本装入矩形(宽x高y) [英] Fitting text into a rectangle (width x by height y) with tkinter

查看:407
本文介绍了使用tkinter将文本装入矩形(宽x高y)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图制作一个程序,根据文本,字体和字体大小,将文本放入一个矩形(x乘y)。

是代码

  def fit_text(screen,width,height,text,font):
measure_frame = Frame(screen )#frame
measure_frame.pack()
measure_frame.pack_forget()
measure = Label(measure_frame,font = font)#做一个空白标签
measure.grid(row = 0,column = 0)#把它放在框架

############################## ############################
#制作一定数量的行
####### ################################################## #

words = text.split()
lines = []
num = 0
previous = 0
while num <= len (words):
measure.config(text =.join(words [previous:num]))#change text
line_width = measure.winfo_width()#获得宽度
print (line_wid th)
如果line_width> = width:#如果行太长了
lines.append(.join(words [previous:num - 1]))#add the last vsion which不是太长
previous = num - 1#以前是不同的
num = num + 1#next word
lines.append(.join(words [previous:]) )#添加其余的
返回\\\
.join(行)

从tkinter导入*
window = Tk()
screen = Canvas (窗口)
screen.pack()
text = fit_text(screen,200,80,我想把这个文本放进一个200像素乘80像素的矩形,(Purisa 12))
screen.create_rectangle(100,100,300,180)
screen.create_text(105,105,text = text,font =(Purisa,12),anchor =nw )

与此问题无关,标签中的文字是由 measure.winfo_width()总是1. ,但它似乎并没有为我工作

你的代码的问题是你正在使用一个控件的宽度,但是这个控件的宽度将会是1,直到这个控件实际上被放置在屏幕上并且被显示出来,因为实际宽度取决于一些因素,直到这种情况才出现。



您不需要将文本放在一个小部件,以衡量它。您可以将字符串传递给 font.measure(),它将返回给定字体中呈现该字符串所需的空间量。



对于python 3.x,您可以像这样导入 Font 类:

  from tkinter.font import Font 

它来自 tkFont 模块:

  from tkFont import Font 

然后,您可以创建一个字体的实例,可以得到有关该字体的信息:

pre $ font size = = font.measure(Hello,world)
printresult:,length

还可以用 font.metrics()方法获取给定字体中的一行的高度,给它一个参数linespace:


$ b $

  height = font.metrics(linespace)


I'm trying to make a program which will fit text into a rectangle (x by y) depending on the text, the font and the font size

Here is the code

def fit_text(screen, width, height, text, font):
    measure_frame = Frame(screen) # frame
    measure_frame.pack()
    measure_frame.pack_forget()
    measure = Label(measure_frame, font = font) # make a blank label
    measure.grid(row = 0, column = 0) # put it in the frame

    ##########################################################
    # make a certain number of lines
    ##########################################################

    words = text.split(" ")
    lines = []
    num = 0
    previous = 0
    while num <= len(words):                
        measure.config(text = " ".join(words[previous:num])) # change text
        line_width = measure.winfo_width() # get the width
        print(line_width)
        if line_width >= width: # if the line is now too long
            lines.append(" ".join(words[previous:num - 1])) # add the last vsion which wasn't too long
            previous = num - 1 # previous is now different
        num = num + 1 # next word
    lines.append(" ".join(words[previous:])) # add the rest of it
    return "\n".join(lines)

from tkinter import *    
window = Tk()
screen = Canvas(window)
screen.pack()
text = fit_text(screen, 200, 80, "i want to fit this text into a rectangle which is 200 pixels by 80 pixels", ("Purisa", 12))
screen.create_rectangle(100, 100, 300, 180)
screen.create_text(105, 105, text = text, font = ("Purisa", 12), anchor = "nw")

The problem with this is no matter what text is in the label the result from measure.winfo_width() is always 1. Here is where I found this from but it doesn't seem to work for me

解决方案

The problem with your code is that you're using the width of a widget, but the width will be 1 until the widget is actually laid out on the screen and made visible, since the actual width depends on a number of factors that aren't present until that happens.

You don't need to put the text in a widget in order to measure it. You can pass a string to font.measure() and it will return the amount of space required to render that string in the given font.

For python 3.x you can import the Font class like this:

from tkinter.font import Font

For python 2.x you import it from the tkFont module:

from tkFont import Font

You can then create an instance of Font so that you can get information about that font:

font = Font(family="Purisa", size=18)
length = font.measure("Hello, world")
print "result:", length

You can also get the height of a line in a given font with the font.metrics() method, giving it the argument "linespace":

height = font.metrics("linespace")

这篇关于使用tkinter将文本装入矩形(宽x高y)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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