python tkinter button颜色变不了,如何更改Tkinter Button周围的框的颜色?

I tried some things I found on Stackoverflow such as placing a frame around the button and giving that a color, like said here. I also tried some other stuff that is said here but I can't get it to work.

I'm using Mac OS and the buttons are rounded, but there's a square around it which makes it look not as nice. Does anyone know how I can get this square to change its color?

This is the code I'm working with:

empty = Button(frame, text='Opnieuw', command=clear, font=bold_font)

empty.config(width=10, fg='#009688', borderwidth=0, relief=RAISED)

empty.grid(row=11, column=0, pady=(25, 0), padx=(80, 0))

This is the square I'm talking about: the white one which is around the button and does not have that green color.

24x37.png

After adding what Bryan Oakley said, by doing this:

empty = Button(frame, text='Opnieuw', command=clear, font=bold_font)

empty.config(width=10, fg='#009688', borderwidth=0, relief=RAISED)

empty.configure(highlightbackground="#009688")

empty.grid(row=11, column=0, pady=(25, 0), padx=(80, 0))

More specifically, this is a larger piece of the code I'm using:

from tkinter import *

from tkinter import font as tkfont

root = Tk()

root.config(background='#009688')

root.title('Contractmaker')

# GUI stuff that takes care of the scrollbar

def on_configure(event):

canvas.configure(scrollregion=canvas.bbox('all'))

def on_mousewheel(event):

canvas.yview_scroll(int(event.delta), 'units')

# Create some fonts

bold_font = tkfont.Font(weight='bold')

# Create the actual GUI

canvas = Canvas(root, width=450, height=550)

canvas.config(background='#009688')

canvas.pack(side=RIGHT)

scrollbar = Scrollbar(root, command=canvas.yview)

# scrollbar.pack(side=RIGHT, fill='y')

canvas.configure(yscrollcommand=scrollbar.set)

canvas.bind('', on_configure)

canvas.bind_all('', on_mousewheel)

frame = Frame(canvas)

frame.config(background='#009688')

canvas.create_window((0,0), window=frame)

empty = Button(frame, text='Opnieuw', font=bold_font)

empty.config(width=10, fg='#009688', borderwidth=0, relief=RAISED)

empty.configure(highlightbackground='#009688')

empty.grid(row=11, column=0, pady=(25, 0), padx=(80, 0))

root.mainloop()

this is what I got:

ta283.png

Does anyone know how I can get the white part of the button to stay white instead of also changing its color? I'm using python 3.8 and Tkinter 8.6.

解决方案

That white area is called the traversal highlight region. It changes color to let the user know when the button has the keyboard focus.

You can change its non-focused color with the highlightbackground option, and its focused color with highlightcolor. You can set it to the color of the background to get it to blend in.

empty.configure(highlightbackground="#009588")

ZUcsP.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值