pythontkintercomboboxttktkinter.checkbutton

How to align the ttk widgets in your application?


I have a tkinter application with 1 CheckButton and 2 ComboBoxes. However, the CheckButton does not seem to start on the same line as the ComboBoxes or vice versa. I have placed them both in column=1 but even that does not seem to fix it.

This is how it looks like:

enter image description here

This is my code:

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

Solution

  • Tkinter widgets that use grid geometry manager, will be set by sticky='' by default, and in this way it will be in the middle of the cell of the column. And the column width will be the width of the widget with greatest width in that column(by default). So if you want to align some widgets, then use stick='w' or any other side on all the required widgets. Or sticky='news' to make it fill the cell entirely.

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

    However I am not so prolific in the way tkinter manages/arranges widgets, the conclusions above were purely drawn from inference. So if I'm wrong, feel free to correct me.