简体   繁体   English

如何更改围绕 Tkinter 按钮的框的颜色?

[英]How to change the color of the box surrounding a 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 .我尝试了一些我在 Stackoverflow 上找到的东西,例如在按钮周围放置一个框架并给它一个颜色,就像这里所说的那样。 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.我使用的是 Mac OS,按钮是圆形的,但它周围有一个正方形,使它看起来不太好。 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.这就是我正在谈论的方块:按钮周围的白色方块,没有那种绿色。

这就是我说的广场

After adding what Bryan Oakley said, by doing this:添加 Bryan Oakley 所说的内容后,通过执行以下操作:

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('<Configure>', on_configure)
canvas.bind_all('<MouseWheel>', 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:这就是我得到的:

在此处输入图像描述

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.我正在使用 python 3.8 和 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 .您可以使用highlightbackground选项更改其非焦点颜色,并使用highlightcolor更改其焦点颜色。 You can set it to the color of the background to get it to blend in.您可以将其设置为背景颜色以使其融入其中。

empty.configure(highlightbackground="#009588")

截屏

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM