簡體   English   中英

在 Python 中使用 Tkinter's.grid() function 時,sticky=“W”未將文本與 window 的左側對齊

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

當我使用.read()讀取文本文件,然后將文本分配給 Tkinter label並將其打包成 window 使用.grid(row, column, sticky="W") window 的左側。 這是代碼:

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"應該將文本與 window 的左側對齊,但它什么也不做,就好像它根本不存在一樣。 有人知道我的代碼有什么問題嗎?

使用左對齊將文本向左對齊,並使用左錨將整個 label 向左對齊。 anchor="w", justify='left' 在 label 內創建不在網格中。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM