简体   繁体   English

如何更改ttk按钮的颜色

[英]How to change the color of ttk button

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.我的问题是我想通过完全更改其背景和前景色来自定义ttk的按钮小部件。 But so far, I have been unsuccessful.但到目前为止,我一直没有成功。

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.请帮我实现我想要的按钮。

Unfortunately, there isn't an easy way to change the foreground of a button from the ttk library.不幸的是,没有一种简单的方法可以从ttk库中更改按钮的前景。 It is always the standard Windows gray like in your picture.它始终是您图片中的标准 Windows 灰色。

But you can easily get what you want with a normal tkinter.Button if you set the right options.但是,如果您设置了正确的选项,您可以使用普通的tkinter.Button轻松获得所需的内容。 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()

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

Although it is not as simple as with Tk buttons, it is possible.虽然它不像使用 Tk 按钮那么简单,但它是可能的。 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.在 ttk 中,如果您将 theme_use 属性设置为以下任何一个:('winnative'、'clam'、'alt'、'default'、'classic'、'vista'、'xpnative'),您应该能够修改默认行为。 I set the "style.map" attribute to avoid background colour change due to mouse hover (The state of the button is always 'active').我设置了“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.希望这可以帮助。

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 all your buttons to the one you "desire", with Python 2.7 and Tkinter 8.6如果您想使用 Python 2.7 和 Tkinter 8.6 将所有按钮更改为您“想要”的按钮,则对我有用

SHORT EXAMPLE:简短示例:

from tkinter import ttk
from tkinter import Tk

root = Tk()
style = ttk.Style()
button_1 = ttk.Button(root, text='click me')
style.theme_use('alt')
style.configure('TButton', font=('American typewriter', 14), background='#232323', foreground='white')
style.map('TButton', background=[('active', '#ff0000')])
button_1.pack()

root.mainloop()

LONG EXAMPLE:长示例:


from tkinter import *
from tkinter import ttk


class App:
    def __init__(self):

        # Window setup
        self.root = Tk()
        self.root.title('BUTTONS')
        WIDTH, HEIGHT = 300, 500
        INITIAL_X_POSITION, INITIAL_Y_POSITION = 450, 200
        self.root.geometry(f'{WIDTH}x{HEIGHT}+{INITIAL_X_POSITION}+{INITIAL_Y_POSITION}')
        self.root.resizable(0, 0)
        self.style = ttk.Style()

        # Layout
        self.button_1 = ttk.Button(self.root, text='click me', command=self.show_me_pi)
        self.style.theme_use('alt')
        self.style.configure('TButton', font=('American typewriter', 14), background='#232323', foreground='white')
        self.style.map('TButton',
                       background=[('active', '#ff0000'), ('disabled', '#f0f0f0')]

                       )
        self.button_1.pack()

        self.button_2 = ttk.Button(self.root, text='click me', state='disabled')
        self.button_2.pack()
        self.root.mainloop()

    def show_me_pi(self):
        py_label = Label(self.root, text='3.14159', font=('American typewriter', 20))
        py_label.pack()


app_runner = App()
import tkinter as tk

btn = tk.fButton(text="Sample", bg = "red") #Refer line 2625 in tkinter code
btn.pack()

For more go to Tkinter code, go to line number 2625.有关更多信息,请转到 Tkinter 代码,请转到第 2625 行。
Here, you will find the solution to the question.在这里,您将找到问题的解决方案。

i used here tk.fButton because in version of tkinter it doesen't support tk.Button , if u are getting error with tk.fButton then use tk.Button left everthing will remain same我这里使用tk.fButton因为Tkinter的版本它doesen't支持tk.Button ,如果u与得到错误tk.fButton然后用tk.Button左寄托都将保持相同

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

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