Python 如何更改ttk按钮的颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27347981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the color of ttk button
提问by maq
I am using Python 3.x on Windows.
我在 Windows 上使用 Python 3.x。
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
通过完全更改其背景和前景色来自定义按钮小部件。但到目前为止,我一直没有成功。
My desired button is:
我想要的按钮是:
I read the ttk.Style
guide and used their code:
我阅读了ttk.Style
指南并使用了他们的代码:
ttk.Style().configure("TButton", padding=6, relief="flat",
background="#000")
btn = ttk.Button(text="Sample")
btn.pack()
But it's changing the border color instead of the whole button bakground. Here is the output:
但它正在改变边框颜色而不是整个按钮背景。这是输出:
Kindly help me achieve my desired button.
请帮我实现我想要的按钮。
采纳答案by maq
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.
不幸的是,没有一种简单的方法可以从ttk
库中更改按钮的前景。它始终是您图片中的标准 Windows 灰色。
But you can easily get what you want with a normal tkinter.Button
if you set the right options. Below is an example script:
但是tkinter.Button
如果你设置了正确的选项,你可以很容易地得到你想要的东西。下面是一个示例脚本:
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()
And here is what it will look like:
这是它的样子:
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 值转换为十六进制,一个简单的技巧是使用str.format
如下:
>>> rgb = (183, 247, 49)
>>> '#{:02x}{:02x}{:02x}'.format(*rgb)
'#b7f731'
>>>
回答by Soltius
import ttk
root.style = ttk.Style()
#root.style.theme_use("clam")
style.configure('TButton', background='black')
style.configure('TButton', foreground='green')
button= ttk.Button(self, text="My background is black and my foreground is green.")
works for me if you want to change allyour buttons to the one you "desire", with Python 2.7 and Tkinter 8.6
如果您想使用 Python 2.7 和 Tkinter 8.6将所有按钮更改为您“想要”的按钮,则对我有用
回答by Ujjwal Mohanty
Although it is not as simple as with Tk buttons, it is possible. In ttk, if you set the theme_use attribute to any of these: ('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative'), you should be able to modify the default behaviour. I set the "style.map" attribute to avoid background colour change due to mouse hover (The state of the button is always 'active').
虽然它不像使用 Tk 按钮那么简单,但它是可能的。在 ttk 中,如果您将 theme_use 属性设置为以下任何一个:('winnative'、'clam'、'alt'、'default'、'classic'、'vista'、'xpnative'),您应该能够修改默认行为。我设置了“style.map”属性以避免由于鼠标悬停而导致的背景颜色变化(按钮的状态始终为“活动”)。
import tkinter as tk
from tkinter import ttk
style = ttk.Style()
style.theme_use('alt')
style.configure('TButton', background = 'red', foreground = 'white', width = 20, borderwidth=1, focusthickness=3, focuscolor='none')
style.map('TButton', background=[('active','red')])
root = tk.Tk()
button = ttk.Button(root,text='Quit')
button.place(relx=0.3,rely=0.4)
root.mainloop()
Hope this helps.
希望这可以帮助。