1

I'm using multiple comboboxes in tkinter, I've passed a list of values to each box.

Currently, after you choose an option you can't clear that box, some selection remains

I want to be able to clear all boxes or have a "blank" value if you want to unselect a box.

So far I've tried assigning a separate button to clear the whole selection but I get errors. The button is assigned this command:

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

I get errors saying: AttributeError: module 'tkinter.ttk' has no attribute 'tk'

EDIT: working code, if anyone needs:

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


BDT
  • 11
  • 1
  • 5
  • The error is becaue the `delete()` method isn't being called on an ***instance*** of the `ttk.Combox` class. – martineau Mar 15 '20 at 15:11
  • What would that look like? I've tried different combinations but so far no luck – BDT Mar 15 '20 at 15:38
  • Updated the code with the combobox code. I've tried with ttk, root instead of self, or leaving it out, or the name of the combobox... – BDT Mar 15 '20 at 15:44
  • Try using `categoriesParts.delete(0, END)` – martineau Mar 15 '20 at 16:07
  • AttributeError: 'NoneType' object has no attribute 'delete' – BDT Mar 15 '20 at 16:09
  • Then the code in your question doesn't accurately reflect your real code because `categoriesParts` shouldn't have the value `None` as the error message indicates. – martineau Mar 15 '20 at 16:15
  • Okay, solved the "NoneType", it was because I had .grid at the end of the combobox. It's now separate. Sees it as a combobox, no errors but it doesn't clear the selections in the GUI, the button has no effect – BDT Mar 15 '20 at 16:15
  • Can't help further without seeing more complete code — or an [mre] that illustrated the problem. – martineau Mar 15 '20 at 16:19
  • Here's something else to try: `categoriesParts.set('')`. – martineau Mar 15 '20 at 16:27
  • Yep, that one works. In the command, it clears the last combobox. I guess because I've created them in a loop, I have to somehow clear them the same... Added the code for the loop – BDT Mar 15 '20 at 16:39
  • Fixed!!! Thank you so much!!!! I added all comboboxes in a list as they were being created, then used a for loop to clear with the .set command <3 – BDT Mar 15 '20 at 17:08
  • Possible duplicate of [How to clear text field part of ttk.Combobox?](https://stackoverflow.com/questions/35233043/how-to-clear-text-field-part-of-ttk-combobox) – martineau Mar 15 '20 at 17:45
  • I saw that thread and didn't find it helpful for my case initially. It only made sense once I went with you through it – BDT Mar 16 '20 at 10:23

0 Answers0