TKinter标签上的图片按钮存在白色边框,点击时仍无法消除
解决Tkinter图片按钮点击时的白色边框问题
你遇到的是Tkinter Button组件默认的激活态高亮/边框问题,即使设置了border=0,点击时的激活态样式仍会显示白色边框。可以通过调整以下几个属性彻底解决:
修改后的完整代码
from tkinter import * import tkinter as tk class CustomButton: def __init__(self): self.root = Tk() self.root.configure(width=470, height=550) self.loadimage = PhotoImage(file="C://Users//UserPC//Pictures//chat2_resized.png") self.labelBottom = Label(self.root, bg="#ABB2B9", height=80) self.labelBottom.place(relwidth=1, rely=0.825) self.roundButton = Button( self.labelBottom, image=self.loadimage, command=lambda: print("Hello World"), border=0, highlightthickness=0, relief="flat", activebackground="#ABB2B9" ) self.roundButton.place(relx=0.77, rely=0.008) self.root.mainloop() win = CustomButton() # 注:原代码中的win.start()会报错,因为CustomButton类未定义start方法,已移除
关键修改说明
highlightthickness=0:取消按钮获得焦点时的高亮边框,彻底隐藏焦点状态下的白色边框relief="flat":将按钮边框样式设为完全平坦,消除默认的凸起/凹陷边框效果activebackground="#ABB2B9":设置按钮激活(点击)时的背景色与Label背景色一致,避免点击时出现白色背景- 移除多余的
pack(side="top"):同时使用pack和place会导致布局冲突,仅保留place定位即可
内容的提问来源于stack exchange,提问作者FulgurWinter X11
相关产品推荐
相关产品推荐

