python tkinter RadioButton color change when clicked

Viewed 123

Hi is it possible to change the background color of a radio button when pressed ? like i have changed the bg and fg and selectcolor but when clicked on the color of the button changes to white and when released it goes back to the selected colors

1 Answers

You can do this by ttk.style.

from tkinter import *
import tkinter.ttk as ttk


root = Tk()                         # Main window
myColor = '#40E0D0'                 # Its a light blue color
root.configure(bg=myColor)          # Setting color of main window to myColor

s = ttk.Style()                     # Creating style element
s.configure('changeBg.TRadiobutton',    # First argument is the name of style. Needs to end with: .TRadiobutton
        background=myColor,         # Setting background to our specified color above
        foreground='black')         # You can define colors like this also

rb1 = ttk.Radiobutton(text = "works :)")       


rb1.pack() 
def change_bg(e):
    rb1['style']='changeBg.TRadiobutton' # Linking style with the button

rb1.bind('<Button-1>',change_bg)
root.mainloop()    

Related