如何更改Tkinter Button周围的框的颜色? [英] How to change the color of the box surrounding a Tkinter Button?

查看:701
本文介绍了如何更改Tkinter Button周围的框的颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试了一些在Stackoverflow上发现的事情,例如在按钮周围放置框架并赋予颜色,例如此处的其他内容但我无法使它正常工作.

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.

我使用的是Mac OS,按钮为圆形,但周围有一个正方形,使其看上去不太舒服.有谁知道我怎么能得到这个正方形来改变它的颜色?

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.

添加布莱恩·奥克利(Bryan Oakley)所说的内容后,这样做:

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('<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()

这就是我得到的:

有人知道我如何才能使按钮的白色部分保持白色,而不是改变其颜色吗?我正在使用python 3.8和Tkinter 8.6.

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.

您可以使用highlightbackground选项更改其非聚焦颜色,并使用highlightcolor更改其聚焦颜色.您可以将其设置为背景颜色以使其融合.

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")

这篇关于如何更改Tkinter Button周围的框的颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆