Reputation: 909
My Tkinter radio buttons are misaligned. I have tried this Python tkinter align radio buttons west, this Python tkinter align radio buttons west and none of them worked, what I see is this
when I use grid to manage the widgets (has to be grid as it is part of a larger UI).
I have tried anchor, and justify but got tracebacks that those were not allowed:
Tracebacks
Traceback (most recent call last):
File "/Users/.../Desktop/tk_gui_grid/temp_1243.py", line 14, in <module>
tk.Radiobutton(root, text='T_Deviation', padx = 20, variable=value, command=get_traj_method, value=0).grid(row=1, anchor=tk.E)
File "/Users/.../opt/anaconda3/lib/python3.7/tkinter/__init__.py", line 2226, in grid_configure
+ self._options(cnf, kw))
_tkinter.TclError: bad option "-anchor": must be -column, -columnspan, -in, -ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky
(base) ... tk_gui_grid % /Users/.../opt/anaconda3/bin/python /Users/.../Desktop/tk_gui_grid/temp_1243.py
Traceback (most recent call last):
File "/Users/.../Desktop/tk_gui_grid/temp_1243.py", line 14, in <module>
tk.Radiobutton(root, text='T_Deviation', padx = 20, variable=value, command=get_traj_method, value=0).grid(row=1, justify=tk.E)
File "/Users/.../opt/anaconda3/lib/python3.7/tkinter/__init__.py", line 2226, in grid_configure
+ self._options(cnf, kw))
_tkinter.TclError: bad option "-justify": must be -column, -columnspan, -in, -ipadx, -ipady, -padx, -pady, -row, -rowspan, or -sticky
Code
import tkinter as tk
root = tk.Tk()
value = tk.IntVar()
value.set(0) # initializing the choice, i.e. mrads
def get_traj_method():
print(value.get())
tk.Label(root, text="""T method:""", justify = tk.LEFT, padx = 20).grid(row=0)
tk.Radiobutton(root, text='T_Deviation', padx = 20, variable=value, command=get_traj_method, value=0).grid(row=1, sticky=tk.E)
tk.Radiobutton(root, text='T_Degrees', padx = 20, variable=value, command=get_traj_method, value=1).grid(row=2, sticky=tk.E)
root.mainloop()
Upvotes: 0
Views: 1525
Reputation: 156
Put sticky = "W", instead of tk.E( tk.W should work too)
The widgets have different sizes, so when locking them to the east(right) the ends align, but the beginnings ( in this case the button) will not be aligned.
When putting sticky = "W" the situation is the reverse. :
import tkinter as tk
root = tk.Tk()
value = tk.IntVar()
value.set(0) # initializing the choice, i.e. mrads
def get_traj_method():
print(value.get())
tk.Label(root, text="T method:", justify = tk.LEFT).grid(row=0)
tk.Radiobutton(root, text='T_Deviation', variable=value, command=get_traj_method, value=0).grid(row=1, sticky="W")
tk.Radiobutton(root, text='T_Degrees', variable=value, command=get_traj_method, value=1).grid(row=2, sticky="W")
root.mainloop()
Upvotes: 3