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

Python 如何在应用程序中对齐ttk小部件?,python,tkinter,combobox,ttk,tkinter.checkbutton,Python,Tkinter,Combobox,Ttk,Tkinter.checkbutton,我有一个tkinter应用程序,其中有1个CheckButton和2个combobox。但是,复选按钮似乎与组合框的起始行不同,反之亦然。我已经将它们都放在column=1中,但即使这样似乎也无法解决问题 这就是它的样子: from tkinter import * from tkinter import ttk owner = ['Spain', 'United Kingdom', 'Malaysia'] vehicleid = ['C161', 'C162'] equipment

我有一个
tkinter
应用程序,其中有1个
CheckButton
和2个
combobox
。但是,
复选按钮
似乎与组合框的起始行不同,反之亦然。我已经将它们都放在
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()

这是我的代码:

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

默认情况下,使用<代码>网格> <代码>几何管理器,默认情况下,将由<代码>粘'= ''/'>代码设置,以这种方式,它将位于列的单元格的中间。列宽将是该列中宽度最大的小部件的宽度(默认情况下)。因此,如果您想对齐一些小部件,那么在所有需要的小部件上使用
stick='w'
或任何其他面。或者
sticky='news'
使其完全填满单元格

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

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

然而,在tkinter管理/安排小部件的方式上,我并不是那么多产,上面的结论纯粹是从推论中得出的。因此,如果我错了,请随时纠正我。

就像我之前所说的,将
sticky=W
添加到
ttk.Combobox
的网格中。您的列长度等于checkbutton的长度,但您的组合框太小。谢谢。你能加入吗?“默认情况下,使用网格几何体管理器的Tkinter小部件将由sticky='n'设置”-这是不正确的。默认值为空字符串,这会导致小部件在其单元格中居中。@BryanOakley好的,谢谢你让我知道,我有一些疑问。它看起来和普通小部件一样。