如何更改ttk按钮的颜色 [英] How to change the color of ttk button

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

问题描述

我在 Windows 上使用 Python 3.x.

我的问题是我想通过完全改变其背景和前景色来自定义 ttk 的按钮小部件.但到目前为止,我一直没有成功.

我想要的按钮是:

我阅读了 像这样:

<预><代码>>>>RGB = (183, 247, 49)>>>'#{:02x}{:02x}{:02x}'.format(*rgb)'#b7f731'>>>

I am using Python 3.x on Windows.

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.

My desired button is:

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

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. It is always the standard Windows gray like in your picture.

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

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 = (183, 247, 49)
>>> '#{:02x}{:02x}{:02x}'.format(*rgb)
'#b7f731'
>>>

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

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