简体   繁体   中英

Align Tkinter radio buttons

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()

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM