如何将文本左对齐?

20

请帮忙修复脚本。

from tkinter import *
colors = ['red',  'white',  'blue']

def packbox(root):
    Label(root, text='Pack').pack()
    for color in colors:
        row = Frame(root)
        lab = Label(row, text=color, relief=RIDGE,  width=25)
        ent = Entry(row, bg=color,   relief=SUNKEN, width=50)
        row.pack(side=TOP,   expand=YES, fill=BOTH)
        lab.pack(side=LEFT,  expand=YES, fill=BOTH)
        ent.pack(side=RIGHT, expand=YES, fill=BOTH)

root = Tk()
packbox(root)
mainloop()

我希望将Label部件中的文本对齐到左边缘

3个回答

36

试试这个

Label(root, text='Pack', anchor='w').pack(fill='both')

1
感谢。如下所示: lab = Label(row, text=color, relief=RIDGE, width=25, anchor='w') - Sergey
15
不仅仅是表达,更好的方式是提供一些解释为什么这样做有效。 - Durga Swaroop
我收到了错误信息“_tkinter.TclError:无法在已由pack管理的.slaves内部使用几何管理器网格”,因此我删除了“.pack()”,它仍然有效。 - gregn3

13

锚点用于定义文本相对于参考点的位置。

以下是可以用于锚点属性的可能常量列表。

NW

N

NE

W

CENTER

E

SW

S

SE

1
我不知道其他的情况,但“CENTER”一定要用小写字母——我刚试过了。 - Andy Brown
错误代码相同:_tkinter.TclError: 错误的锚点“W”:必须是n,ne,e,se,s,sw,w,nw或center。 - ArduinoBen
Ajay 可能把它和 grid(粘性布局)混淆了,后者确实需要大写字母。 - ArduinoBen

2
以下代码将在按下白色按钮、红色按钮和蓝色按钮时分别打开一个新窗口,每个按钮都向左对齐。在每个按钮的方法中,还有一个名为“关闭窗口”的附加按钮,用于关闭每次点击按钮时打开的新窗口。请保留HTML标签。
from Tkinter import*

import Tkinter as tk


class Packbox(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)


        bottomframe = Frame(root)


        bottomframe.pack( side = BOTTOM )


        # Initialize buttons redbutton, whitebutton and bluebutton



        whitebutton = Button(self, text="Red", fg="red", command=self.white_button)
        whitebutton.pack( side = LEFT)

        redbutton = Button(self, text="white", fg="white",  command=self.red_button)
        redbutton.pack( side = LEFT )



        bluebutton = Button(self, text="Blue", fg="blue", command=self.blue_button)
        bluebutton.pack( side = LEFT )

        self.white_button()
        self.red_button()
        self.blue_button()

        # Define each buttons method, for example,  white_button() is whitebutton's method, which
        # is called by command=self.white_button


    def white_button(self):

        self.top = tk.Toplevel(self)

        # Creates new button that closes the new window that is opened when one of the color buttons
        # are pressed. 
        button = tk.Button(self.top, text="Close window", command=self.top.destroy)

        # prints the text in the new window that's opened with the whitebutton is pressed

        label = tk.Label(self.top, wraplength=200,text="This prints white button txt")


        label.pack(fill="x")
        button.pack()


    def red_button(self):

        self.top = tk.Toplevel(self)
        button = tk.Button(self.top, text="Close window", command=self.top.destroy)

        label = tk.Label(self.top, wraplength=200,text="This prints red button txt")

        label.pack(fill="x")
        button.pack()




    def blue_button(self):

        self.top = tk.Toplevel(self)
        button = tk.Button(self.top, text="Close window", command=self.top.destroy)

        label = tk.Label(self.top, wraplength=200,text="This prints blue button txt")


        label.pack(fill="x")

        button.pack()



if __name__ == "__main__":
    root = tk.Tk()
    Packbox(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接