Python 如何将文本左对齐?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21495367/
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-18 23:00:06  来源:igfitidea点击:

how to align text to the left?

pythontkinter

提问by Sergey

please help to fix the script.

请帮助修复脚本。

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()

I would like to align the text in the Label widget on the left edge

我想在左边缘的 Label 小部件中对齐文本

采纳答案by afkfurion

Try this

尝试这个

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

回答by user1749431

The following opens a new window with text for each of the buttons whitebutton, redbutton and bluebutton when they are pressed, the buttons are all aligned LEFT, and in each button's method there is an additional button named "Close window" that closes the new window that is opened with each button click.

下面打开一个新窗口,其中每个按钮 whitebutton、redbutton 和 bluebutton 在按下时都带有文本,这些按钮都向左对齐,并且在每个按钮的方法中,还有一个名为“关闭窗口”的附加按钮,用于关闭新窗口每次单击按钮都会打开。

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()

回答by Goulouh Anwar

Anchors are used to define where text is positioned relative to a reference point.

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

Here is list of possible constants, which can be used for Anchor attribute.

这是可能的常量列表,可用于 Anchor 属性。

NW

N

NE

W

CENTER

E

SW

S

SE