我正在尝试使用 customtkinter 创建一个统计框架,但遇到了左对齐文本切入边框的问题:
import customtkinter as ctk
window = ctk.CTk()
ctk.set_appearance_mode('light')
main_frame = ctk.CTkFrame(window, fg_color='white')
main_frame.pack(fill=ctk.BOTH, expand=True)
text_frame = ctk.CTkFrame(main_frame, fg_color='white', border_color='black', border_width=2)
text_frame.pack_propagate(False)
text_frame.pack(side=ctk.TOP)
# add a hello world label
hello_label = ctk.CTkLabel(text_frame, text="Hello, World!", font=('TkDefaultFont', 24), anchor='w', justify='left')
hello_label.pack(fill='x')
hello_label = ctk.CTkLabel(text_frame, text="Hello, World!", font=('TkDefaultFont', 24), anchor='w', justify='left')
hello_label.pack(fill='x')
hello_label = ctk.CTkLabel(text_frame, text="Hello, World!", font=('TkDefaultFont', 24), anchor='w', justify='left')
hello_label.pack(fill='x')
window.mainloop()
如何在几行左对齐文本周围绘制边框而不让文本切入边框?
要使任何小部件不粘在边框上,您可以使用填充
您可以将
padx
和 pady
添加到包(或网格)方法中,如下所示:hello_label.pack(fill='x', padx=3, pady=3)
您的完整代码现在是:
import customtkinter as ctk
window = ctk.CTk()
ctk.set_appearance_mode('light')
main_frame = ctk.CTkFrame(window, fg_color='white')
main_frame.pack(fill=ctk.BOTH, expand=True)
text_frame = ctk.CTkFrame(main_frame, fg_color='white', border_color='black', border_width=2)
text_frame.pack_propagate(False)
text_frame.pack(side=ctk.TOP)
# add a hello world label
hello_label = ctk.CTkLabel(text_frame, text="Hello, World!", font=('TkDefaultFont', 24), anchor='w', justify='left')
hello_label.pack(fill='x', padx=3, pady=3)
hello_label = ctk.CTkLabel(text_frame, text="Hello, World!", font=('TkDefaultFont', 24), anchor='w', justify='left')
hello_label.pack(fill='x', padx=3, pady=3)
hello_label = ctk.CTkLabel(text_frame, text="Hello, World!", font=('TkDefaultFont', 24), anchor='w', justify='left')
hello_label.pack(fill='x', padx=3, pady=3)
window.mainloop()
唯一的问题是,由于填充,标签之间的间距有点大。
希望我能帮到你,祝你有美好的一天