简体   繁体   中英

python tkinter RadioButton color change when clicked

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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