1

我试图根据我的设备中的物品CheckButton创建一个。已创建,但并非其中的所有项目都在应用程序中可见。是不是因为我的物品太大了?tkinterlistCheckButtonequipment

这是我的代码:

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

这是 tkinter 应用程序的外观:

在此处输入图像描述

4

3 回答 3

2

发生这种情况,因为在:

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

您将每个 Checkbutton 打包到相同的位置。因此,在每次迭代中,之前的 Checkbutton 都会被替换,并且只显示最后一项 ( 'Streamcell highspeed collision extra large version 8.5') 的 Checkbutton。

例如,要更正此问题,您可以这样做:

for i,x in enumerate(equipment):
    dd_equipment = Checkbutton(window, text=x, variable=x)
    dd_equipment.grid(row=2+i, column=1, sticky=W)
于 2021-06-02T08:20:49.987 回答
1

您的代码永远不会更改展示位置的行或列。因此它们都重叠。这里有2个可能的解决方案

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

for val,x in enumerate(equipment):
    dd_equipment = Checkbutton(window, text=x, variable=x)
    dd_equipment.grid(row=val, column=1)
于 2021-06-02T08:26:14.983 回答
0

发生这种情况是因为您将所有复选按钮放置/打包到同一位置,因此要避免这种情况,而不是:

dd_equipment.grid(row=2, column=1)

你可以做:

dd_equipment.grid(row=i+2, column=1, sticky=W)
于 2021-06-02T08:44:19.157 回答