如何使用 Tkinter 创建填充窗口宽度的换行文本? [英] How do I use Tkinter to create line-wrapped text that fills the width of the window?

查看:74
本文介绍了如何使用 Tkinter 创建填充窗口宽度的换行文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Label 小部件不换行.Message 小部件将换行文本,但强制它大致呈方形.举个例子:

The Label widget doesn't line-wrap. The Message widget will line-wrap text, but forces it to be roughly square. Here's an example:

from Tkinter import *

root = Tk()
root.title("hello")

Message(root, text=48*'xxxxx ').grid(row=0, column=0, columnspan=3)

Label(root, text='Name:').grid(row=1, column=0)
Entry(root, width=50).grid(row=1, column=1)
Button(root, text="?").grid(row=1, column=2)

Button(root, text="Left").grid(row=2, column=0)
Button(root, text="Center").grid(row=2, column=1)
Button(root, text="Right").grid(row=2, column=2)

root.mainloop()

我知道我可以使用 aspect=700 来改变形状,但是像这样的硬编码数字是我试图避免的.

I know that I can use aspect=700 to change the shape, but hard-coding numbers like that is what I'm trying to avoid.

推荐答案

Tkinter Label 小部件会自动换行.只是默认设置是不换行.要使文本在一个包装上设置 wraplength 参数,此单位是屏幕单位,因此请尝试 wraplength=50 并根据需要进行调整.您还需要将 justify 设置为 LEFTRIGHTCENTER.

The Tkinter Label widget does wrap. It is just that the default setting is no wrapping. To get the text on one to wrap set the wraplength parameter, the units for this are screen units so try wraplength=50 and adjust as necessary. You will also need to set justify to LEFT, RIGHT, or CENTER.

这篇关于如何使用 Tkinter 创建填充窗口宽度的换行文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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