简体   繁体   English

如何在 Tkinter 中对齐标签中的文本

[英]How to justify text in label in Tkinter

In Tkinter in Python: I have a table with a different label.在 Python 中的 Tkinter:我有一个带有不同标签的表。 How can I justify the text that is in the label?如何证明标签中的文本对齐? Because It is a table and the texts in different labels come together!因为它是一个表格,不同标签中的文字汇集在一起​​!

from tkinter import *
root=Tk()
a=Label(root,text='Hello World!')
a.pack()
a.place(x=200,y=200)
b=Label(root,text='Bye World')
b.pack()
b.place(x=200,y=100)

I want something for justifying in center some text in label but it is not something that I need plz check this: link我想要一些东西来证明标签中的一些文本居中对齐,但这不是我需要的东西,请检查这个:链接

By default, the text in a label is centered in the label.默认情况下,标签中的文本在标签中居中。 You can control this with the anchor attribute, and possibly with the justify attribute.您可以使用anchor属性来控制它,也可以使用justify属性。 justify only affects the text when there is more than one line of text in the widget. justify仅在小部件中有不止一行文本时才影响文本。

For example, to get the text inside a label to be right-aligned you can use anchor="e" :例如,要使标签内的文本右对齐,您可以使用anchor="e"

a=Label(root,text='Hello World!', anchor="e")

Note, however, this will appear to have no effect if the label is exactly big enough to hold the text.但是请注意,如果标签大到足以容纳文本,这似乎不起作用。 In your specific example, you would need to give each label the same width:在您的具体示例中,您需要为每个标签提供相同的宽度:

a=Label(..., width=12)
b=Label(..., width=12)

To add on to what Bryan said, LEFT is the constant you are looking for to correctly format your wrapped text.补充一下 Bryan 所说的, LEFT是您正在寻找的常量,以正确设置换行文本的格式。 You can also justify to the RIGHT or CENTER (the default).您还可以对齐RIGHTCENTER (默认值)。

a=Label(root,text='Hello World!', anchor="e", justify=LEFT)

instead of using .pack() i would use .grid() http://effbot.org/tkinterbook/grid.htm而不是使用 .pack() 我会使用 .grid() http://effbot.org/tkinterbook/grid.htm

grid will allow better management of your components网格将允许更好地管理您的组件

find bellow an example of usage and management:在下面找到一个使用和管理的例子:

Label(root, text="First").grid(row=0, sticky=W)
Label(root, text="Second").grid(row=1, sticky=W)

entry1 = Entry(root)
entry2 = Entry(root)

entry1.grid(row=0, column=1)
entry2.grid(row=1, column=1)

checkbutton.grid(columnspan=2, sticky=W)

image.grid(row=0, column=2, columnspan=2, rowspan=2,
           sticky=W+E+N+S, padx=5, pady=5)

button1.grid(row=2, column=2)
button2.grid(row=2, column=3)

you would endup using the grid option padx="x" to "justify" your labels你最终会使用网格选项 padx="x" 来“证明”你的标签

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM