Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deselecting from a listbox in Tkinter

I'm just wondering how I can deselect from a list box in thinter. Whenever I click on something in a list box, it gets highlighted and it gets underlined, but when I click off of the screen towards the side, the list box selection stays highlighted. Even when I click a button, the selection still stays underlined. For ex: in the example code below, I can't click off of the list box selection after clicking on one of them.

from tkinter import *

def Add():
   listbox.insert(END, textVar.get())


root = Tk()

textVar = StringVar()
entry = Entry(root, textvariable = textVar)
add = Button(root, text="add", command = Add)
frame = Frame(root, height=100, width=100, bg="blue")
listbox = Listbox(root, height=5)
add.grid(row=0, column=0, sticky=W)
entry.grid(row=0, column=1, sticky=E)
listbox.grid(row=1, column=0)
frame.grid(row=1, column=1)

root.mainloop()
like image 560
Mustafa Ali Avatar asked Sep 03 '25 04:09

Mustafa Ali


2 Answers

Yes, that's the normal behavior of the listbox. If you want to change that you could call the clear function every time the listbox left focus:

listbox.bind('<FocusOut>', lambda e: listbox.selection_clear(0, END))
like image 52
Novel Avatar answered Sep 05 '25 21:09

Novel


Use the selectmode parameter on the Listbox widget. You can click the selected item again and it will clear the selection.

See the effbot link: http://effbot.org/tkinterbook/listbox.htm

listbox = Listbox(root, height=5, selectmode=MULTIPLE)
like image 44
M Laing Avatar answered Sep 05 '25 19:09

M Laing