tkinter columnconfigure将我的标签文本设置为居中

时间:2017-08-04 14:04:30

标签: python tkinter grid

我试图找出如何左对齐我的标签文字,当我设置列配置以便我的控件随着窗口调整而增大文本/控件在中间但是我喜欢文字从左边开始。我是tkinter的新手,所以我可能会遗漏一些明显的东西。我将有几个控件,所以我使用网格布局。感谢您提供任何指导。

from tkinter import *
from tkinter import filedialog

class Application(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.geometry("800x600")
        self.grid()

        self.label_ccdata_path = Label(master, text='My message goes here')
        self.label_ccdata_path.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W)

root = Tk()
root.columnconfigure(0, weight=1)
app = Application(root)
app.mainloop()

1 个答案:

答案 0 :(得分:0)

您可以使用anchor

将标签的anchor = "w"设置为西部

我修改了以下代码:

from tkinter import *
from tkinter import filedialog

class Application(Frame):

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.master.geometry("800x600")
        self.grid()

        self.label_ccdata_path = Label(master, text='My message goes here', anchor = "w")
        self.label_ccdata_path.grid(row=0, column=0, columnspan=3, sticky=N+S+E+W)

root = Tk()
root.columnconfigure(0, weight=1)
app = Application(root)
app.mainloop()