Home > Net >  In tkinter, unable put label to the left, It remains in the middle
In tkinter, unable put label to the left, It remains in the middle

Time:11-17

I mean i am reading a txt file, For every line in txt file i want a new label.


root = tk.Tk()

#here it opens the file
with open("/file.txt", "r") as openedFile:

    allLines = openedFile.readlines()
    for line in allLines:
        textInLine = line

        testText = Label(root, text=textInLine)
        testText.pack()

root.geometry("400x300")
root.mainloop()

Here in the text file lengths of lines is not same, so it puts the label text horizontally centered, i want it sticking to the left most side.

CodePudding user response:

you can use anchor attribute:

testText.pack(anchor="w")

w means 'west'

CodePudding user response:

There are 3 ways how you can manage your widgets.

First one is by packing like:

testText = Label(root, text="Test Text")
testText.pack(anchor=W)

Second one would be by placing (absolute positioning):

testText2 = Label(root, text="Test Text 2")
testText2.place(x=0, y=50, anchor=W)

And the third one would be grids.

Its a bit more complicated but probably the best one for managing widgets by position and dynamicaly to the window geometry. More about grids you can read here

  • Related