首页 > 解决方案 > python中的锚文本

问题描述

我正在尝试使用锚将标签中的文本向右对齐,如下所示:

label=Label(root, text="some text", anchor='e', width=50)

它适用于一条线。但由于某种原因,当文本长于一行时,它仅适用于最长的行,但其他行相对于最长的行居中。为什么会这样?我该如何解决? 例子

标签: pythonpython-3.xtkinterlabelalignment

解决方案


所以我认为你的问题是你正在使用anchor而不是justify.

这两者在操作的文本行数上有所不同。第一个影响一行文本,而后者影响多行文本。

所以我试了一下,然后:

from tkinter import *


root = Tk()

myContainer1 = Frame(root)  
myContainer1.grid()         

label1 = Label(root, text = "Lorem Ipsum is simply dummy text of the printing and typesetting industry.\n" +
               "Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer\n" +
               "took a galley of type and scrambled it to make a type specimen book.\n" +
               "It has survived not only five centuries, but also the leap into electronic typesetting,\n"+
               "remaining essentially unchanged.",justify = 'right', width = 100 )

label1.grid(row = 0, column= 0)
root.mainloop()   

因此,这有效并证明了东方的文本。

希望这可以帮助!


推荐阅读