sticky="W" not aligning text to left side of window while using Tkinter's .grid() function in Python

Viewed 713

When I use .read() to read a text file and then I assign the text to a Tkinter label and pack it into a window using .grid(row, column, sticky="W"), the text does not get aligned to the left of the window. Here's the code:

import tkinter as tk
instructions_file = open("instructions.txt")
instructions = tk.Tk()
instructions.title("Instructions")
instruction_lbl = tk.Label(
                master=instructions,
                text=instructions_file.read()
                ).grid(row=1, column=1, sticky="W")

I checked this code several times but I can't figure out what's going wrong. sticky="W" should align the text to the left side of the window, but it does nothing, as if it wasn't even there. Anyone knows what's wrong with my code?

1 Answers

Use Justify Left for aligning text to left and anchor left for aligning the whole label to left. anchor="w", justify='left' inside label creation not in the grid.

import tkinter as tk
instructions_file = open("instructions.txt")
instructions = tk.Tk()
instructions.title("Instructions")
instruction_lbl = tk.Label(
                master=instructions,
                text=instructions_file.read(), anchor="w", justify='left'
                ).grid(row=1, column=1, sticky="W")
Related