简体   繁体   English

python tkinter 单击时 RadioButton 颜色变化

[英]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就像我已经更改了 bg 和 fg 以及 selectcolor 但是当点击按钮的颜色变为白色并且当释放时它返回到选定的 colors

You can do this by ttk.style .你可以通过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()    

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

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