1

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?

Natalie Clarius
  • 429
  • 5
  • 14

1 Answers1

1

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()
Thingamabobs
  • 7,274
  • 5
  • 21
  • 54
  • Thanks for your answer, and sorry, I was including a too simplified version of my actual code; see the update. My problem is that I have several radio buttons (so there is no one `rb`) and I can not find a way to access which exact button is the one selected and to be configured. – Natalie Clarius Aug 19 '20 at 16:20
  • @lemontree updated my answer, let me know if something missing or you have questions about. – Thingamabobs Aug 19 '20 at 16:26
  • 1
    Oh, this makes sense. I had tried out something similar, but specified the command already upon instantiation and ran into the problem that I obviously could not use the `rb` variable which was just being defined, and failed to see that I could simply `config` `rb` after the initial assignment. This works. Thank you! – Natalie Clarius Aug 19 '20 at 16:34
  • I modified your `select` funcion such that additionally, all other radio buttons that are now no longer selected are changed back to `fg=black`: Save every `rb` into a list `rbs` upon creation, then in `select`, add `for rb_ in rbs: if rb_ != rb: rb_.config(fg=black)`. – Natalie Clarius Aug 19 '20 at 16:36
  • Is this how you would edit it? Or could you edit it by yourself? I would accept it. – Thingamabobs Aug 19 '20 at 16:45
  • 1
    Yes, that's it; perfect. – Natalie Clarius Aug 19 '20 at 16:50