Trey Jackson
Trey Jackson

Reputation: 74460

Forcing radiobutton to use the diamond as the indicator, Tcl/Tk 8.6

In the process of porting an application from Tcl/Tk 8.4 to 8.6. For historic reasons, we desire to keep the radio button indicators to have the shape of a diamond. From the 8.6 docs, it appears that this should be possible:

A radiobutton is a widget that displays a textual string, bitmap or image and a diamond 
or circle called an indicator.

However, I'm unable to figure out how to get the radio button to use the diamond.

One possibility is to use ttk::radiobutton instead, however, it's not a drop-in replacement. The ttk::radiobutton does not recognize the -anchor option, which we currently use.

Upvotes: 0

Views: 329

Answers (1)

Donal Fellows
Donal Fellows

Reputation: 137707

The indicator shape depends on the platform; the exact rendering is usually done in a way that approximately follows the host platform's style guidelines (as they stood at the time that the code was written, alas). Indicator shape isn't exposed as a controllable feature to the Tk API, as the control of how it is done is via which rendering code is compiled at the C level. Changing that is really messy, and you really ought to ask yourselves whether you're really needing that old Motif look (that was where those diamonds came from).

The Ttk widgets with the right theme do much better (the classic theme has the diamond), but move some of their layout functionality into the style system precisely because things like anchoring are often specified by platform style guides. We can override… but the documentation for Ttk is very poor. However, the tkdocs style tutorial is a lot better. In particular, we can use it to see that we can make a derived style that will apply the extra options that you want and that you can apply where you want.

# Switch to classic style
ttk::style theme use classic

# Make the style. Derived because of the . and the existing style name
ttk::style configure RightAnchor.TRadiobutton -anchor e

# Make some widgets that use the style
ttk::radiobutton .example1 -variable foo -value foo -text "This is an example" \
    -style RightAnchor.TRadiobutton
ttk::radiobutton .example2 -variable foo -value bar -text "This is also an example" \
    -style RightAnchor.TRadiobutton
# This one doesn't use the style, for comparison
ttk::radiobutton .example3 -variable foo -value boo -text "This is not an example"
pack .example1 .example2 .example3 -fill x

I'm not certain that this is going to work for you — Ttk is intensely complicated and has a lot of features where it simply doesn't provide as much feedback as I'd like — but it is the best idea I've got. If this doesn't work, your main option would be to make a megawidget (probably using a canvas) that does what you really want, but that's a lot of work.


Are your diamonds really necessary? Normal user expectations of radiobuttons on different platforms may not expect them any more.

Upvotes: 1

Related Questions