当我使用.read()
读取文本文件,然后将文本分配给Tkinter label
并使用.grid(row, column, sticky="W")
将其打包到窗口中时,文本不会与窗口左侧对齐。这是代码:
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")
我检查了这个代码好几次,但我不知道出了什么问题。sticky="W"
应该将文本与窗口的左侧对齐,但它什么也不做,就好像它根本不在那里一样。有人知道我的代码出了什么问题吗?
使用"左对齐"将文本向左对齐,使用"左锚"将整个标签向左对齐。anchor=";w";,justify='left'在标签创建内部,而不是在网格中。
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")