Skip to content
Advertisement

How to create a proper CheckButton in Tkinter?

Im trying to create a CheckButton in tkinter based on the items I have in my equipment list. The CheckButton has been created but not all the items that are in equipment are visible in the app. Is it because my items are to large?

This is my code:

from tkinter import *

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
dd_owner = StringVar(window)
dd_owner.set(owner[0]) # the first value
w = OptionMenu(window, dd_owner, *owner)
w.grid(row=1, column=1)

dd_id = StringVar(window)
dd_id.set(vehicleid[0])
w0 = OptionMenu(window, dd_id, *vehicleid)
w0.grid(row=0, column=1)

#This is the CheckButton
dd_equipment = Checkbutton
for x in equipment:
    dd_equipment = Checkbutton(window, text=x, variable=x)
    dd_equipment.grid(row=2, column=1)

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

##These are the titles
l1 = 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()

This is how the tkinter app looks like:

enter image description here

Advertisement

Answer

This happens, because in:

for x in equipment:
    dd_equipment = Checkbutton(window, text=x, variable=x)
    dd_equipment.grid(row=2, column=1)

you are packing every Checkbutton to the same position. Hence, with every iteration, the previous Checkbutton is replaced and only the one for your last item ('Streamcell highspeed collision extra large version 8.5') shows.

To correct this, you could, for example, do this instead:

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