I am trying to create a stats frame with customtkinter, and I am running into the problem of left-justified text cutting into the border:
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()
How can I draw a border around several lines of left justified text without the text cutting into the border?
To make a any widget not stick to a border, you could use padding
You can add padx
and pady
to a pack (or grid) method like so: hello_label.pack(fill='x', padx=3, pady=3)
Your full code is now:
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()
The only problem is that labels are spaced a little bit because of the padding.
Hope I helped you, have a nice day