sticky=“W”在Python中使用Tkinter的.grid()函数时,未将文本对齐到窗口左侧

2024-10-01 11:19:58 发布

您现在位置:Python中文网/ 问答频道 /正文

当我使用.read()读取文本文件,然后将文本分配给Tkinterlabel,并使用.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"应该将文本与窗口的左侧对齐,但它什么也不做,就好像它根本不在那里一样。有人知道我的代码出了什么问题吗


Tags: 代码文本importreadtkinterascolumntk
1条回答
网友
1楼 · 发布于 2024-10-01 11:19:58

使用“左对齐”将文本向左对齐,使用“左定位”将整个标签向左对齐。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")

相关问题 更多 >