tkinter文本对齐大量文本

时间:2017-11-12 10:44:09

标签: python python-3.x tkinter

如何在tkinter python中保留文本对齐大部分文本?

我尝试过锚定和粘性,但无济于事。

这是我的代码:

import tkinter as tk

lots_of_text = "Lorem ipsum dolor sit amet, consectetur adipiscing \
elit. Donec a rhoncus elit, ac mattis quam. Vestibulum ex enim, dictum \
vitae finibus sed, pharetra ac tellus. Integer in mollis lectus. \n\
ipsum nunc, venenatis sit amet augue mattis, faucibus blandit est. \
Vestibulum vel porta lorem. Quisque pretium sodales velit, non molestie \
rhoncus sed. Integer mattis eros nec porttitor auctor. \n\ 
Vivamus porta faucibus mi ac interdum. Morbi lobortis ipsum venenatis, \
fringilla justo vitae, dignissim elit."

root = tk.Tk()
info = tk.Label(root, text=lots_of_text)
info.pack()

root.mainloop()

由于

1 个答案:

答案 0 :(得分:2)

您可以使用关键字参数char msg_hex[4] = {0x61, 0x62, 0x63, 0x64};
您也可以使用三重引号来表示多行句子。

justify='left'

可替换地:

import tkinter as tk

lots_of_text = """Lorem ipsum dolor sit amet, consectetur adipiscing \
elit. Donec a rhoncus elit, ac mattis quam. Vestibulum ex enim, dictum \
vitae finibus sed, pharetra ac tellus. Integer in mollis lectus. \n\
ipsum nunc, venenatis sit amet augue mattis, faucibus blandit est. \
Vestibulum vel porta lorem. Quisque pretium sodales velit, non molestie \
rhoncus sed. Integer mattis eros nec porttitor auctor. \n\ 
Vivamus porta faucibus mi ac interdum. Morbi lobortis ipsum venenatis, \
fringilla justo vitae, dignissim elit."""

root = tk.Tk()
info = tk.Label(root, text=lots_of_text, justify='left')
info.pack()

root.mainloop()