Tkinter Frame控件设置边框后不显示问题求助
解决Tkinter Frame边框不显示的问题
你的Frame边框看不到主要有两个原因:
- 高亮边框(highlight)被隐藏:
highlightthickness和highlightcolor设置后,默认highlightbackground和主窗口背景色一致,导致高亮边框和背景融合,肉眼无法识别。 - relief边框未生效:
relief=tk.RIDGE需要配合bd(borderwidth,默认值为0)使用,否则不会显示边框样式。
两种修复方案:
方案一:修复高亮边框显示
给Frame添加highlightbackground属性,设置为与highlightcolor不同或相同的颜色,让高亮边框显现:
import time import threading import tkinter as tk from tkinter import ttk, PhotoImage def main(): main_win = tk.Tk() main_win.title("Functional Pomodoro Timer") main_win.resizable(True,True) main_win.minsize(400, 200) main_win.geometry("800x400") # 添加highlightbackground属性,与highlightcolor区分开 frame1 = tk.Frame(main_win, highlightthickness=20, highlightcolor="black", highlightbackground="white", relief=tk.RIDGE) frame1.place(x=0, y=0, relwidth=0.5, relheight=0.5) time_label = tk.Label(frame1, text="00:00", font=("Arial", 24)) time_label.place(relwidth=0.5, relheight=0.5) main_win.mainloop() if __name__ == "__main__": main()
方案二:使用bd+relief实现边框
这是更常用的Frame边框实现方式,直接设置bd(边框宽度)和relief样式:
import time import threading import tkinter as tk from tkinter import ttk, PhotoImage def main(): main_win = tk.Tk() main_win.title("Functional Pomodoro Timer") main_win.resizable(True,True) main_win.minsize(400, 200) main_win.geometry("800x400") # 设置bd(边框宽度)和relief样式 frame1 = tk.Frame(main_win, bd=20, relief=tk.RIDGE, bg="white") frame1.place(x=0, y=0, relwidth=0.5, relheight=0.5) time_label = tk.Label(frame1, text="00:00", font=("Arial", 24)) time_label.place(relwidth=0.5, relheight=0.5) main_win.mainloop() if __name__ == "__main__": main()
内容的提问来源于stack exchange,提问作者user21708146
相关产品推荐
相关产品推荐

