3

I am creating a Listbox using Tkinter and Python. I want to make a Button for select all, but I can't find any info regarding selecting elements using code.

 self.l = Listbox(self, height=12, selectmode=MULTIPLE)
 self.selectAll=Button(self, text="select all",
                      command=self.selectAllCallback())
 def selectAllCallback(self)
 # What to do here

1 Answer 1

7

You can use selection_set (or select_set) method with 0 and END as arguments.

For example, try following code:

from Tkinter import *

def select_all():
    lb.select_set(0, END)

root = Tk()
lb = Listbox(root, selectmode=MULTIPLE)
for i in range(10): lb.insert(END, i)
lb.pack()
Button(root, text='select all', command=select_all).pack()
root.mainloop()

In the following statement, you are calling self.selectAllCallback, not bind it with button click. It is called before the button is generated.

self.selectAll=Button(self,text="select all", command=self.selectAllCallback())
                                                                            ^^

It should be:

self.selectAll=Button(self, text="select all", command=self.selectAllCallback)
Sign up to request clarification or add additional context in comments.

2 Comments

I used it but nothing happens when i click select all, nothing is highlighted and nothing happened.
Thanks, that must have been hard to find.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.