对齐Tkinter单选按钮

问题描述

我的Tkinter单选按钮未对齐。我已经尝试过这个Python tkinter align radio buttons west,这个Python tkinter align radio buttons west,但都没有用,我看到的是这个

enter image description here

当我使用网格来管理小部件时(必须是网格,因为它是较大的UI的一部分)。

我已经尝试过锚定,并进行辩解,但得到的回溯是不允许的:

跟踪

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",justify=tk.E)
  File "/Users/.../opt/anaconda3/lib/python3.7/tkinter/__init__.py",kw))
_tkinter.TclError: bad option "-justify": must be -column,or -sticky

代码

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,sticky=tk.E)
tk.Radiobutton(root,text='T_degrees',value=1).grid(row=2,sticky=tk.E)

root.mainloop()

解决方法

放置粘性=“ W”,而不是tk.E(tk.W也应该起作用)

窗口小部件的大小不同,因此将其锁定在东边(右)时,它们的末端对齐,但开始时(在这种情况下为按钮)将不对齐。

当置入粘性=“ W”时,情况则相反。 :

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',value=1).grid(row=2,sticky="W")

root.mainloop()