-1

I'm trying to make a simple search listbox which will have quite a few variables in it therefore making it necessary to have a 'clear selected' function. For this I've created a 'Clear' button and tried to link it using tk's selection_clear function. Unfortunately I cannot get it to work. Could you please point me in the right direction?

Code extract:

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
        self.create_widgets()

    def create_widgets(self):
        top = self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=0)

        parameters_list_1 = 'foo', 'bar', 'foofoo', 'barbar', 'foobar'
        self.sb1_values = tk.Variable(value=parameters_list_1)
        self.search_listbox = tk.Listbox(self, activestyle='dotbox', listvariable=self.sb1_values, selectmode=tk.MULTIPLE)
        self.search_listbox.grid(row=3, column=1, sticky=tk.N+tk.S+tk.E+tk.W)

        self.clear_button = tk.Button(self, text='Clear', command=self.search_listbox.selection_clear(0, tk.END))
        self.clear_button.grid(row=4, column=2, sticky=tk.N+tk.S+tk.E+tk.W)

app = Application()
app.master.title('GUI')
app.mainloop()

Thank you in advance

OParker
  • 265
  • 1
  • 5
  • 19
  • Can you replace `command=self.search_listbox.selection_clear(0, tk.END)` by `command=lambda : self.search_listbox.selection_clear(0, tk.END)` ? – Sylvain Biehler Apr 19 '18 at 09:28
  • Ahhh knew it would be something that simple. Thank you Sylvain that worked! Could you pop and answer down then I'll accept it. – OParker Apr 19 '18 at 09:31

2 Answers2

2

Replace command=self.search_listbox.selection_clear(0, tk.END) by command=lambda : self.search_listbox.selection_clear(0, tk.END)

Sylvain Biehler
  • 1,403
  • 11
  • 25
1

Create a separate function to clear it -

class Application(tk.Frame):
    def __init__(self, master=None):
        tk.Frame.__init__(self, master)
        self.grid(sticky=tk.N+tk.S+tk.E+tk.W)
        self.create_widgets()

    def create_widgets(self):
        top = self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.rowconfigure(0, weight=1)
        self.columnconfigure(0, weight=0)

        parameters_list_1 = 'foo', 'bar', 'foofoo', 'barbar', 'foobar'
        self.sb1_values = tk.Variable(value=parameters_list_1)
        self.search_listbox = tk.Listbox(self, activestyle='dotbox', listvariable=self.sb1_values, selectmode=tk.MULTIPLE)
        self.search_listbox.grid(row=3, column=1, sticky=tk.N+tk.S+tk.E+tk.W)

        self.clear_button = tk.Button(self, text='Clear', command=self.clear_search)
        self.clear_button.grid(row=4, column=2, sticky=tk.N+tk.S+tk.E+tk.W)

    def clear_search(self):
        print(tk.END)
        self.search_listbox.selection_clear(0, tk.END)



app = Application()
app.master.title('GUI')
app.mainloop()
Aritesh
  • 1,985
  • 1
  • 13
  • 17
  • Thank you for this Aritesh, I'm going to use Sylvains answer however but I appreciate the input – OParker Apr 19 '18 at 09:35