【问题标题】:customtkinter - align text in CTkLabelcustomtkinter - 在 CTkLabel 中对齐文本
【发布时间】:2022-01-07 00:56:36
【问题描述】:

我希望我的文本向左(或向西)对齐。

我的代码:

import customtkinter as ctk
import tkinter as tk

root = tk.Tk()
root.geometry("300x100")

label = ctk.CTkLabel(master=root,
 text="this is a label",
 text_color="#fff",
 bg_color="green",
 width=250,
 justify="left",
 anchor="w"
)

label.place(x=0, y=0)
root.mainloop()

即使使用justify="left"anchor="w" 也没有任何变化,并且文本居中。为什么?我该如何解决?

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    实际上CTKLabelFrame,文本是该框架内的Label。使用place(relx=0.5, rely=0.5, anchor='c') 将文本放置在框架的中心。所以传递给CTKLabeljustifyanchor 选项不起作用。

    但是,您可以在创建实例后移动文本,如下所示:

    label = ctk.CTkLabel(master=root,
     text="this is a label",
     text_color="#fff",
     bg_color="green",
     width=250,
     justify="left",
     anchor="w"
    )
    label.text_label.place(relx=0, anchor='w') # move the text to the left side of frame
    label.place(x=0, y=0)
    

    或者使文本水平填充框架:

    label.text_label.place(relwidth=1)
    

    【讨论】:

      猜你喜欢
      • 2022-12-04
      • 2011-12-26
      • 2020-07-12
      • 1970-01-01
      • 2014-02-06
      • 2015-12-31
      • 2016-04-15
      • 2012-01-21
      • 2021-11-01
      相关资源
      最近更新 更多