You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Python3的Tkinter中设置按钮的默认边框颜色(非悬停状态)

Fixing Tkinter Button Border Colors in Python 3

Hey there! Let's tackle those Tkinter button border issues you're dealing with. I see you’ve already tried some code, so first let’s break down why that isn’t working, then walk through two reliable solutions to get the default border color you want.

Why Your Current Code Isn’t Delivering the Default Border

Your code uses highlightcolor, highlightbackground, and highlightthickness—but these control the focus highlight ring (the border that pops up when the button is selected via keyboard or click), not the button’s static default border. The native tk.Button doesn’t have a direct bordercolor attribute for the non-hover state, which is why your search didn’t turn up a straightforward fix. Let’s fix that.

Approach 1: Simulate a Border with a Frame (Simplest Solution)

This is the easiest way to get a consistent custom border color that stays visible in the default state. We’ll wrap the button in a colored frame to mimic a border:

from tkinter import *

root = Tk()
root.title('Calculator')
root.config(bg='black')

# Create a frame to act as the button's border
border_frame = Frame(root, bg='blue', padx=2)  # padx = border thickness
# Create the button inside the frame, no native border
x = Button(border_frame, text='tgrsj', padx=50, bg='black', fg='lime', bd=0, relief=FLAT)
x.pack()

# Place the framed button in your window
border_frame.grid(row=0, column=0)

root.mainloop()
  • The border_frame uses bg='blue' as your desired border color, and padx=2 adjusts how thick the border is.
  • The button itself has bd=0 (removes its native border) and relief=FLAT to avoid extra styling conflicts.
  • This gives you a static, unchanging border that stays visible in the default, non-hover state.

Approach 2: Use ttk.Button with Custom Styles (For Full State Control)

If you want to customize border colors across different button states (default, hover, pressed), use ttk.Button (the themed Tkinter button) and define a custom style:

from tkinter import *
from tkinter import ttk

root = Tk()
root.title('Calculator')
root.config(bg='black')

# Create a custom style object
style = ttk.Style()
style.theme_use('clam')  # Use a theme that supports full custom styling

# Configure the default state style
style.configure('Custom.TButton',
                background='black',
                foreground='lime',
                borderwidth=2,
                bordercolor='blue',
                relief='solid')

# Optional: Adjust hover state styling if needed
style.map('Custom.TButton',
          bordercolor=[('active', 'darkblue')])  # Darker border on hover

# Create the button with your custom style
x = ttk.Button(root, text='tgrsj', style='Custom.TButton', padding=50)
x.grid(row=0, column=0)

root.mainloop()
  • We define a Custom.TButton style, setting borderwidth and bordercolor explicitly for the default state.
  • The map method lets you tweak styles for other states (like hover) if you want, but your core need—the default border color—is handled in configure.
  • Note: Stick to themes like clam or alt for the most reliable custom styling support.

Bonus: Keep Your Focus Binding with a Custom Border

If you want to retain the focus behavior from your original code while adding a default border, combine the frame approach with your bindings:

from tkinter import *

root = Tk()
bindings = {
    '<FocusIn>': {'default': 'active'},
    '<FocusOut>': {'default': 'active'}
}
for k, v in bindings.items():
    root.bind_class('Button', k, lambda e, kwarg=v: e.widget.config(**kwarg))

root.title('Calculator')
root.config(bg='black')

border_frame = Frame(root, bg='blue', padx=2)
x = Button(border_frame, text='tgrsj', padx=50, bg='black', fg='lime', bd=0, relief=FLAT)
x.pack()
border_frame.grid(row=0, column=0)

root.mainloop()

内容的提问来源于stack exchange,提问作者user14599750

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 07:26:01