Python Forum
[Tkinter] list box select event triggers when selecting another listbox
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] list box select event triggers when selecting another listbox
#1
Hi !

I have 2 list box.

i have attached event with both list box.

but after 1st click in both list box, when i click any list box both event triggered.

can some one have any idea

here is event code & declaring of both list box

import tkinter as tk

root = tk.Tk()
label = tk.Label(root)

listbox = tk.Listbox(root)
listbox1 = tk.Listbox(root)

label.pack(side="bottom", fill="x")

listbox.pack(side="top", fill="both", expand=True)
listbox1.pack(side="bottom", fill="both", expand=True)

listbox.insert("end", "one", "two", "three", "four", "five")
listbox1.insert("end", "one", "two", "three", "four", "five")

def callback(event):
    print("1")

def callback1(event1):
    print("2")

listbox.bind("<<ListboxSelect>>", callback)
listbox1.bind("<<ListboxSelect>>", callback1)

root.mainloop()
Reply
#2
An empty selection is being triggered when changing another listbox.
Issue30200: tkinter ListboxSelect
You can add a check for the current selection in the event handler and return if it is empty.
import tkinter as tk
 
root = tk.Tk()
label = tk.Label(root)
 
listbox = tk.Listbox(root)
listbox1 = tk.Listbox(root)
 
label.pack(side="bottom", fill="x")
 
listbox.pack(side="top", fill="both", expand=True)
listbox1.pack(side="bottom", fill="both", expand=True)
 
listbox.insert("end", "one", "two", "three", "four", "five")
listbox1.insert("end", "one", "two", "three", "four", "five")
 
def callback(event):
    if not event.widget.curselection():
        return
    print("1")
 
def callback1(event1):
    if not event1.widget.curselection():
        return
    print("2")

listbox.bind("<<ListboxSelect>>", callback)
listbox1.bind("<<ListboxSelect>>", callback1)

root.mainloop()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  PysimpleGUI - listbox with list jamesaarr 2 4,303 Sep-06-2021, 09:52 AM
Last Post: jamesaarr
  [Tkinter] listbox constantly being added to the list. speedev 4 2,446 Jun-08-2021, 08:16 PM
Last Post: speedev
  populate list with images and be able to select them ricardons 0 2,135 Jan-11-2019, 03:45 PM
Last Post: ricardons
  How to format a list when displaying in a tkinter Listbox nortski 3 9,624 Apr-03-2018, 02:31 AM
Last Post: Larz60+
  [PyQt] Selecting lines in a list BigMan 0 2,824 Jan-05-2017, 04:27 PM
Last Post: BigMan

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020