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:
JavaScript
x
45
45
1
from tkinter import *
2
3
owner = ['Spain', 'United Kingdom', 'Malaysia']
4
vehicleid = ['C161', 'C162']
5
equipment = ['X-tron senistra beamer mode version 5.4433',
6
'Loadcell compact designer 2000 version 32.44',
7
'Streamcell highspeed collision extra large version 8.5']
8
9
window = Tk()
10
window.title("Running Python Script") # Create window
11
window.geometry('550x300') # Geo of the window
12
13
##These are the option menus
14
dd_owner = StringVar(window)
15
dd_owner.set(owner[0]) # the first value
16
w = OptionMenu(window, dd_owner, *owner)
17
w.grid(row=1, column=1)
18
19
dd_id = StringVar(window)
20
dd_id.set(vehicleid[0])
21
w0 = OptionMenu(window, dd_id, *vehicleid)
22
w0.grid(row=0, column=1)
23
24
#This is the CheckButton
25
dd_equipment = Checkbutton
26
for x in equipment:
27
dd_equipment = Checkbutton(window, text=x, variable=x)
28
dd_equipment.grid(row=2, column=1)
29
30
##The run button
31
run_list_button =Button(window, text="Send data of ID's to database!")
32
run_list_button.grid(column=0, row=3)
33
34
##These are the titles
35
l1 = Label(window, text='Select Owner', width=15)
36
l1.grid(row=1, column=0)
37
38
l0 = Label(window, text='Select vehicle id:', width = 30)
39
l0.grid(row=0, column=0)
40
41
l0 = Label(window, text='Select equipment:', width = 30)
42
l0.grid(row=2, column=0)
43
44
mainloop()
45
This is how the tkinter app looks like:
Advertisement
Answer
This happens, because in:
JavaScript
1
4
1
for x in equipment:
2
dd_equipment = Checkbutton(window, text=x, variable=x)
3
dd_equipment.grid(row=2, column=1)
4
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:
JavaScript
1
4
1
for i,x in enumerate(equipment):
2
dd_equipment = Checkbutton(window, text=x, variable=x)
3
dd_equipment.grid(row=2+i, column=1, sticky=W)
4