My thought is to try amending the ttk.RadioButton's layout elements or element's option. See http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/ttk-layouts.html.
The layout of its element seems to consist of a label, that is inside the focus, that is inside indicator, that is inside the padding (See below variable rbLayout). The Radiobutton.indicator's side option value is left. The Radiobutton.label's anchor option appears blank. I think changing either of these options may get what you want. You would also have to add the option "style=your_customed_stylename" into your ttk.Radiobutton declaration.
>>> import tkinter.ttk as ttk
>>> s = ttk.Style()
>>> rb = ttk.Radiobutton(None, text='RB1')
>>> rbClass = rb.winfo_class()
>>> rbClass
'TRadiobutton'
>>> rbLayout = s.layout('TRadiobutton')
>>> rbLayout
[('Radiobutton.padding', {'sticky': 'nswe', 'children': [('Radiobutton.indicator', {'sticky': '', 'side': 'left'}), ('Radiobutton.focus', {'children': [('Radiobutton.label', {'sticky': 'nswe'})], 'sticky': '', 'side': 'left'})]})]
>>> type(rbLayout)
<class 'list'>
Update:
- I think the rbLayout variable shows that by default the
Radiobutton.padding contains two children elements, i.e.
Radiobutton.indicator and Radiobutton.focus, and they are positioned
on the left side of Radiobutton.padding. Furthermore,
Radiobutton.focus contains Radiobutton.label. Correction to my
earlier explanation.
- To achieve the described layout, I think Radiobutton.indicator's
'side' key should have value 'top' and 'sticky' key should have
value 'n'. Furthermore, Radiobutton.focus's 'side' key should have
value 'bottom' and 'sticky' value should be 's'.
- I created a script to implemented the changes. See below code.
However, it did not work. I am surprise the radiobutton layout did
not change. Have not understood why it did not work.
Hope someone more knowledgeable can explain why the amendments to Radiobutton element's layout did not change to the required layout.
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
root.geometry("300x100")
s = ttk.Style()
m = s.layout('TRadiobutton')
print('TRadiobutton.Layout : Default')
print(m , "\n")
# Change to default Radiobutton.indicator elements
list(list(m[0])[1]['children'][0])[1]['side'] = 'top'
list(list(m[0])[1]['children'][0])[1]['sticky'] = 'n'
# Change to default Radiobutton.focus elements
list(list(m[0])[1]['children'][1])[1]['side']='bottom'
list(list(m[0])[1]['children'][1])[1]['sticky']='s'
print('TRadiobutton.Layout : Amended')
print(m, "\n")
frame1 = ttk.Frame(root)
frame1.grid()
rb1 = ttk.Radiobutton(frame1, text="Button 1")
rb1.grid()
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
Update 2 : SOLUTION
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
root.geometry("300x100")
s = ttk.Style()
s.theme_use('default')
print('TRadiobutton.Layout : Default')
print(s.layout('TRadiobutton'), "\n")
s.layout('TRadiobutton',
[('Radiobutton.padding',
{'children':
[('Radiobutton.indicator', {'side': 'top', 'sticky': ''}), # Just need to change indicator's 'side' value
('Radiobutton.focus', {'side': 'left',
'children':
[('Radiobutton.label', {'sticky': 'nswe'})],
'sticky': ''})],
'sticky': 'nswe'})])
print('TRadiobutton.Layout : Amended')
print(s.layout('TRadiobutton'), "\n")
frame1 = ttk.Frame(root)
frame1.grid()
rb1 = ttk.Radiobutton(frame1, text="Button 1")
rb1.grid()
root.rowconfigure(0, weight=1)
root.columnconfigure(0, weight=1)
A big thanks :) to j_4321 for his latest solution building on my suggested approach. I have implemented it (see above) to my previous code and it works. I like to highlight the following:
- My code shows changes made to the layout of the Radiobutton's default style 'TRadiobutton' instead of creating a new Style with a changed layout. The advantage of this difference appears to overcome the shortcoming mention by j_4321 2nd solution. That is, it works on all ttk themes ('clam', 'alt', 'default', 'classic') and not just on 'clam' and 'alt'. At least this works on my Linux 16.04 platform.
- The mistake in my earlier code was that I had created a new instance of s.layout() (i.e. m = s.layout()) and changed it's layout, instead of actually changing the layout of the default Radiobutton style s.layout('TRadiobutton'). I learned from j_4321's solution that one can change the layout of a default style by adding the changed details of a widget's layout as the 2nd term in the tuple ttk.Style.layout(widget's default style name), i.e. ttk.Style.layout(widget's default style name e.g. 'TRadiobutton', [changed layout details of widget]).
- To change the Radiobutton indicator's location to appear at the top of it's label, change to the Radiobutton.indicator's 'side' value to 'top' is only needed. Changes to the Radiobutton.focus's 'side' value had no effect on Radiobutton's layout.