1
votes

I have Radiobutton displayed in sunken button look that I would like to have:

  • black text on white background when not selected,
  • white text on dark gray background when selected.

Currently I only have gray background but no white text when selected, which makes for poor contrast.

enter image description here

for (lbl, val) in [("A", "a"), ("B", "b"))]:
    rb = tk.Radiobutton(tab,
                        text=lbl,
                        variable=v,
                        value=val,
                        command=select,
                        selectcolor=gray,
                        indicatoron=0,
                        width=25, pady=7.5)
    rb.pack(...)

tk.Radiobutton has an option to configure selectcolor, which is the background color when selected, but it seems to offer no such option for the foreground color when selected.

I thought that one might achieve this by specifying a command triggered on selection that will rb.config the foreground on that radiobutton which is selected, but this would require accessing externally the properties of radio buttons themselves, rather than just the value of the variable they set, which I found no way to do so far.

How do I achieve an option along the lines of selectforeground?

1
wouldnt rb.config(foreground='white') this work?Cool Cloud
@Cool Cloud No, because I only what white text for whichever button is selected, not all of them.lemontree
oh, you have more than one.Cool Cloud
Yes, the code snippet in my first version of the post was too heavily simplified.lemontree

1 Answers

1
votes

Use a annonymous function for this:

import tkinter as tk

def select(rb):
    rb.config(foreground='white')
    for rb_ in rbs:
        if rb_ != rb:
           rb_.config(fg='black')

root = tk.Tk()

rbs=[]

def do_buttons():
    for _ in range(11):
        v=tk.IntVar()
        val = 1

        rb = tk.Radiobutton(root,
                            text="A",
                            variable=v,
                            value=val,
                            selectcolor='gray',
                            indicatoron=0,
                            width=25, pady=7.5)
        rb.config(command=lambda arg=rb:select(arg))

        rb.pack()
        rbs.append(rb)

b = tk.Button(root, text='click', command=do_buttons)
b.pack()

root.mainloop()