如何在python中的tkinter中对齐标签中的文本需要在tkinter中对齐

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37318060/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:13:19  来源:igfitidea点击:

how to justify text in label in tkinter in python Need justify in tkinter

pythontkinteralignment

提问by A.Elhami

In Tkinter in Python: I have a table with a different label. How can I justify the text that is in the label? Because It is a table and the texts in different labels come together!

在 Python 中的 Tkinter:我有一个带有不同标签的表。如何证明标签中的文本对齐?因为它是一个表格,不同标签中的文字汇集在一起​​!

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 Bryan Oakley

By default, the text in a label is centered in the label. You can control this with the anchorattribute, and possibly with the justifyattribute. justifyonly affects the text when there is more than one line of text in the widget.

默认情况下,标签中的文本在标签中居中。您可以使用anchor属性来控制它,也可以使用属性来控制它justifyjustify仅当小部件中的文本多于一行时才影响文本。

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)

回答by glls

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)
entry1 = 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" 来“证明”你的标签