如何改变TTK按钮的颜色 [英] How to change the color of ttk button
问题描述
我在Windows上使用Python 3.x都有。
I am using Python 3.x on Windows.
我的问题是我想通过完全改变它的背景和前景颜色来定制 TTK
的一个按钮控件。但到目前为止,我都没有成功。
My problem is I want to customize a button widget of ttk
by completely changing its background and foreground color. But so far, I have been unsuccessful.
我想要的按钮是:
我阅读 ttk.Style
引导和利用他们的code:
I read the ttk.Style
guide and used their code:
ttk.Style().configure("TButton", padding=6, relief="flat",
background="#000")
btn = ttk.Button(text="Sample")
btn.pack()
但它改变了边框颜色,而不是整个按钮bakground。下面是输出:
But it's changing the border color instead of the whole button bakground. Here is the output:
请帮我实现我想要的按钮。
Kindly help me achieve my desired button.
推荐答案
不幸的是,没有一个简单的方法来改变从 TTK
库按钮的前景。它始终是标准的Windows灰色就像在你的图片。
Unfortunately, there isn't an easy way to change the foreground of a button from the ttk
library. It is always the standard Windows gray like in your picture.
但是你可以很容易地得到你想要一个正常的 tkinter.Button
如果你设置了正确的选项。下面是一个示例脚本:
But you can easily get what you want with a normal tkinter.Button
if you set the right options. Below is an example script:
import tkinter as tk
root = tk.Tk()
btn = tk.Button(root,
bg='#000000',
fg='#b7f731',
relief='flat',
text='hello button',
width=20)
btn.pack()
root.mainloop()
这是它的样子:
此外,绿色我挑阴凉处只是一个例子一个我认为是pretty接近你想要的东西。但是,你可以指定任何你想要的十六进制颜色code。如果你需要把一个RGB值为十六进制,一个简单的诀窍是使用 str.format
像这样:
Also, the shade of green I picked was just an example one that I thought was pretty close to what you wanted. But you can specify any hex color code you want. If you need to turn a RGB value into hex, a simple trick is to use str.format
like so:
>>> rgb = (183, 247, 49)
>>> '#{:02x}{:02x}{:02x}'.format(*rgb)
'#b7f731'
>>>
这篇关于如何改变TTK按钮的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!