如何将文本左对齐?

2024-10-02 00:40:49 发布

您现在位置:Python中文网/ 问答频道 /正文

请帮助修复脚本。

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

我想在标签小部件的左边对齐文本


Tags: textlabrootfillsidelabelpackyes
3条回答

锚定用于定义相对于参照点放置文字的位置。

下面是可能的常量列表,可用于锚定属性。

NW

N

NE

W

CENTER

E

SW

S

SE

下面将打开一个新窗口,其中包含每个按钮的文本whitebutton、redbutton和 蓝色按钮按下时,按钮都左对齐,按每个按钮的方法 还有一个名为“关闭窗口”的附加按钮,用于关闭每次单击按钮时打开的新窗口。

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

试试这个

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

相关问题 更多 >

    热门问题