Custom Tkinter中border_color无法显示完整边框问题求助
Custom Tkinter CTkFrame 边框显示不完整问题
问题说明
设置CTkFrame的border_width=3和border_color="white"后,白色边框未能完整环绕整个Frame,显示效果不符合预期。
涉及代码
# 显示提示信息的函数 def message(self): # 向用户展示重要提示 self.message_frame = ctk.CTkFrame(self.parent.functions_frame, border_width=3, border_color="white") self.message_frame.place(relx=0.7, rely=0.05) self.message_title = ctk.CTkLabel( self.message_frame, text="Important:", font=ctk.CTkFont(size=17, weight="bold") ) self.message_title.grid(row=0, column=0) self.messages = ctk.CTkLabel(self.message_frame, text="""*Where you can enter a mark you can also update a mark for the same student by entering a different one""", text_color="red") self.messages.grid(row=1, column=0)
解决方法
1. 替换布局方式(推荐)
place布局不会自动调整Frame尺寸以适配内部控件,导致Frame可能比内容小,边框显示不全。改用grid或pack布局,它们会自动让Frame包裹内部元素:
# 替换place为pack,可根据需求调整padx/pady self.message_frame.pack(relx=0.7, rely=0.05, anchor="nw", padx=5, pady=5) # 或者用grid(需确保父容器用grid布局) self.message_frame.grid(row=0, column=1, padx=5, pady=5)
2. 手动适配Frame尺寸(保留place布局时)
如果必须用place,先让Frame更新布局,再根据内容需求设置尺寸:
self.message_frame.place(relx=0.7, rely=0.05) # 强制更新布局以获取内容所需尺寸 self.message_frame.update_idletasks() # 设置Frame为内容所需的宽高 self.message_frame.configure( width=self.message_frame.winfo_reqwidth(), height=self.message_frame.winfo_reqheight() )
3. 给内部控件添加间距
内部Label紧贴边框也会让边框看起来不完整,给grid布局添加padx和pady:
self.message_title.grid(row=0, column=0, padx=10, pady=(10, 5)) self.messages.grid(row=1, column=0, padx=10, pady=(5, 10))
内容的提问来源于stack exchange,提问作者Govind
相关产品推荐
相关产品推荐

