首页 > 解决方案 > 如何清除 tkinter 组合框?

问题描述

我在 tkinter 中使用了多个组合框,我已将值列表传递给每个框。

目前,在您选择一个选项后,您无法清除该框,一些选择仍然存在

如果您想取消选择一个框,我希望能够清除所有框或具有“空白”值。

到目前为止,我已经尝试分配一个单独的按钮来清除整个选择,但我得到了错误。该按钮分配有以下命令:

for c in categories:
    ttk.Label(text=c, relief=tk.RIDGE, width=15).grid(row=r,column=0)
    categoriesParts = ttk.Combobox(root, textvariable = listVariables[r], values=listMakers[r],width=10, state="readonly")
    categoriesParts.grid(row=r,column=1)
    r = r + 1

def clearComboBox():
    categoriesParts.delete(0, END)

browseButton_Clear = tk.Button(text='Clear Config', command=clearComboBox, bg='green', fg='white')

我收到错误消息: AttributeError: module 'tkinter.ttk' has no attribute 'tk'

编辑:工作代码,如果有人需要:

list_of_widgets = []

for c in categories:
    ttk.Label(text=c, relief=tk.RIDGE, width=15).grid(row=r,column=0)
    categoriesParts = ttk.Combobox(root, textvariable = listVariables[r], values=listMakers[r], width=10, state="readonly")
    categoriesParts.grid(row=r,column=1)
    list_of_widgets.append(categoriesParts)
    r = r + 1

def clearComboBox():
    for widget in list_of_widgets:
        widget.set('')

browseButton_Clear = tk.Button(text='Clear Config', command=clearComboBox, bg='green', fg='white')


标签: pythontkintercombobox

解决方案


推荐阅读