Tkinter 小部件在同一行,但为什么它们不那样显示?

Tkinter widgets are in the same row, but why aren't they appearing that way?

我正在尝试为搜索引擎制作一个开始菜单。我试图让按钮和其他小部件出现在标签旁边。这是我的代码:

class Application(tk.Tk): #subclass Tk
    '''Does most of the handling.'''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.title("Academic Search")
        self.geometry("1000x600")
        self.resizable(width=True,height=True)
        self.start = Start(self)
        self.start.grid(sticky=(tk.E + tk.W + tk.N + tk.S))
        self.update_button = ttk.Button(self, text="Update Database", command=self.to_database)
        self.update_button.grid(row=0,column=0,sticky=tk.W)
        self.cc_button = ttk.Button(self, text="Find Common Citations",command=self.to_cc)
        self.cc_button.grid(row=1, column=0, sticky=tk.W)#, pady=20)
        self.search_button = ttk.Button(self, text="Search", command=self.search)
        self.search_button.grid(row=2, column=2, sticky=(tk.E))


class Start(tk.Frame): #subclass of Frame
    '''The start window'''
    def __init__(self, parent, *args, **kwargs): #takes same args as parent
        '''Initializes start window.'''
        super().__init__(parent, *args, **kwargs) #calls superclass constructor
        query = tk.StringVar()
        self.num_return = tk.IntVar()
        cw = tk.DoubleVar()
        cd = tk.IntVar()
        graph = tk.BooleanVar()
        jstor = tk.BooleanVar()
        self.mb_text = tk.StringVar()
        self.mb_text.set('Search multiple DOIs')
        self.num_return = tk.IntVar()
        self.search_box = ttk.Entry(parent, text='Enter one DOI:',textvariable=query)
        self.multiple_button = ttk.Button(self, textvariable=self.mb_text,command=self.to_mult)
        self.lbl = ttk.Label(self,text='Enter one DOI.')
        self.num_return_lbl = ttk.Label(self, text='Maximum number of records returned:')
        self.num_return_box = ttk.Entry(parent, textvariable=self.num_return)
        self.cw_lbl = ttk.Label(self, text='Citation weight (must be between 0 and 1):')
        self.cw_box = ttk.Spinbox(parent, from_=0, to=1, increment=0.01,textvariable=cw)
        self.cd_lbl = ttk.Label(self, text='Maximum citation distance (runtime will increase dramatically if greater than 3):')
        self.cd_box = ttk.Spinbox(parent, from_=1, to=10,increment=1, textvariable=cd)
        self.jstor_button = ttk.Checkbutton(parent, text='Use JSTOR (requires JSTOR Labs token)',command=jstor)
        self.graph_box = ttk.Checkbutton(parent, text='Graph', variable=graph)
        self.add_widgets()

    def add_widgets(self):
        '''Adds widgets to grid.'''
        self.lbl.grid(row=2, column=1, sticky=tk.E)
        self.cw_lbl.grid(row=3, column=1, sticky=tk.E)
        self.cw_box.grid(row=3, column=2, sticky=tk.E)
        self.cd_lbl.grid(row=4, column=1, sticky=tk.E)
        self.cd_box.grid(row=4, column=2, sticky=tk.E)
        self.num_return_lbl.grid(row=5, column=1, sticky=tk.E)
        self.num_return_box.grid(row=5, column=2, sticky=tk.E)
        self.search_box.grid(row=2, column=2, sticky=(tk.E+tk.W))
        self.graph_box.grid(row=6, column=2, sticky=tk.E)
        self.jstor_button.grid(row=7, column=2, sticky=tk.E)
        self.multiple_button.grid(row=9, column=0, sticky=(tk.W+tk.N))

为什么标签与位于同一行的其他元素(旋转框、复选按钮、条目)不一致?

这是我做的时候的样子 mainloop():

按钮和标签有不同的父级,这就是它们出现在不同位置的原因。

如果给 Start 框架一个独特的颜色,你可以很清楚地看到这一点。例如,将创建 Start 的方式更改为:

self.start = Start(self, background="red")

您最终会得到一个 window,如下所示:

查看这两行代码中的第一个参数。一种使用 self,一种使用 parentselfparent 是两个不同的小部件。

    self.cw_lbl = ttk.Label(self, ...)
    self.cw_box = ttk.Spinbox(parent, ...)

Start 中任何地方使用 parent 作为小部件的父级,您应该将其更改为 self。比如上面的代码需要改成:

self.cw_box = ttk.Spinbox(self, ...)