如何对齐应用程序中的 ttk 小部件?

How to align the ttk widgets in your application?

我有一个 tkinter 应用程序,其中包含 1 CheckButton 和 2 ComboBoxes。但是,CheckButton 似乎与 ComboBoxes 不在同一行开始,反之亦然。我已将它们都放在 column=1 中,但即便如此似乎也无法解决问题。

是这样的:

这是我的代码:

from tkinter import *
from tkinter import ttk
    
owner = ['Spain', 'United Kingdom', 'Malaysia']
vehicleid = ['C161', 'C162']
equipment = ['X-tron senistra beamer mode version 5.4433', 
            'Loadcell compact designer 2000 version 32.44',
            'Streamcell highspeed collision extra large version 8.5']

window = Tk()
window.title("Running Python Script") # Create window
window.geometry('550x300') # Geo of the window

##These are the option menus
w = ttk.Combobox(window, values=owner)
w.current(0)
w.grid(row=1, column=1)

w0 = ttk.Combobox(window, values=vehicleid)
w0.current(0)
w0.grid(row=0, column=1)

for i,x in enumerate(equipment):
    dd_equipment = Checkbutton(window, text=x, variable=x)
    dd_equipment.grid(row=2+i, column=1, sticky=W)
    if i == 0 or i == 1:
        dd_equipment.toggle()

##The run button 
run_list_button =ttk.Button(window, text="Send data of ID's to database!")
run_list_button.grid(column=0, row=5)

##These are the titles
l1 = ttk.Label(window, text='Select Owner', width=15)
l1.grid(row=1, column=0)

l0 = Label(window, text='Select vehicle id:', width = 30)
l0.grid(row=0, column=0)

l0 = Label(window, text='Select equipment:', width = 30)
l0.grid(row=2, column=0)

mainloop()

使用 grid 几何管理器的 Tkinter 小部件,默认情况下将由 sticky='' 设置,这样它将位于列单元格的中间。列宽将是该列中宽度最大的小部件的宽度(默认情况下)。所以如果你想对齐一些小部件,然后在所有需要的小部件上使用 stick='w' 或任何其他边。或者 sticky='news' 使其完全填满单元格。

w .grid(row=1, column=1, sticky='w')

w0.grid(row=0, column=1, sticky='w')

不过我对tkinter manages/arranges widgets的方式不是很精通,以上结论纯属推理。所以如果我错了,请随时纠正我。