简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM