Search code examples
python-3.xcolorstkinterradio-buttonttk

Changing the background color of a radio button with tkinter in Python 3


I added a few radio buttons to a GUI I'm working on in Python 3 using ttk, but there is a white square around them which doesn't match the blue background of the rest of the GUI.

I've tried background= ..., foreground= ..., bg= ..., fg= ..., and a few other things in ttk.Radiobutton(). It works fine with labels and other things... what am I missing?


Solution

  • ttk doesn't support arguments such as "background", "foreground", "font" on its Radiobutton, but it does support styles. Example code (python 3.4):

    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('Wild.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 :)", style = 'Wild.TRadiobutton')       # Linking style with the button
    
    rb1.pack()                          # Placing Radiobutton
    
    root.mainloop()                     # Beginning loop