如何在 Tkinter 中创建一个合适的 CheckButton?
How to create a proper CheckButton in Tkinter?
我正在尝试根据我的设备 list
中的物品在 tkinter
中创建一个 CheckButton
。 CheckButton
已创建,但并非 equipment
中的所有项目都在应用程序中可见。是不是我的东西太大了?
这是我的代码:
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 应用程序的样子:
发生这种情况,因为在:
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)
您的代码绝不会更改展示位置的行或列。因此它们都重叠。这里有 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)
发生这种情况是因为您 placing/packing 所有复选按钮都在同一位置,所以要避免这种情况,而不是:
dd_equipment.grid(row=2, column=1)
你可以做到:
dd_equipment.grid(row=i+2, column=1, sticky=W)
我正在尝试根据我的设备 list
中的物品在 tkinter
中创建一个 CheckButton
。 CheckButton
已创建,但并非 equipment
中的所有项目都在应用程序中可见。是不是我的东西太大了?
这是我的代码:
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 应用程序的样子:
发生这种情况,因为在:
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)
您的代码绝不会更改展示位置的行或列。因此它们都重叠。这里有 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)
发生这种情况是因为您 placing/packing 所有复选按钮都在同一位置,所以要避免这种情况,而不是:
dd_equipment.grid(row=2, column=1)
你可以做到:
dd_equipment.grid(row=i+2, column=1, sticky=W)