How to change ttk button color

I am using Python 3.x for Windows.

My problem is that I want to customize the ttk button ttk by completely changing the background and foreground color. But so far I have not been successful.

My desired button:

enter image description here

I read the ttk.Style and used my code:

 ttk.Style().configure("TButton", padding=6, relief="flat", background="#000") btn = ttk.Button(text="Sample") btn.pack() 

But that changes the border color, not the whole bakground button. Here is the result:

enter image description here

Please help me achieve the desired button.

+6
source share
2 answers

Unfortunately, there is no easy way to change the foreground of a button in the ttk library. This is always the standard gray Windows, as in the picture.

But you can easily get what you want with the usual tkinter.Button if you set the correct parameters. The following 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 this is how it will look:

enter image description here

Also, the shade of green that I chose was just an example that I thought was very close to what you wanted. But you can specify any desired hex color code. If you need to rotate the RGB value to hex, a simple trick is to use str.format like this:

 >>> rgb = (183, 247, 49) >>> '#{:02x}{:02x}{:02x}'.format(*rgb) '#b7f731' >>> 
+7
source
 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 need with Python 2.7 and Tkinter 8.6

+2
source

Source: https://habr.com/ru/post/979255/


All Articles