Tkinter ListBox Virtual binding applied to radio selection
The <<ListboxSelect>>
virtual event is generated when the Listbox performs a selection operation, from which you can set up event handlers.
Tkinter ListBox Virtual binding applied to radio selection Example#1
When selecting an item in the Listbox, the selected item can be listed above.
from tkinter import *
def itemSelected(event): # List the selected single item
obj = event.widget # Get the object of the event, i.e. the Listbox control object
index = obj.curselection() # Get Index
print(index)
print(type(obj.get(index)))
print(obj.get(index))
var.set(obj.get(index)) # Set label content
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x250")
var = StringVar()
# var.set("cxq123")
lab = Label(root,text="",textvariable=var)
lab.pack(pady=5)
lb = Listbox(root)
for fruit in fruits:
lb.insert(END,fruit)
lb.bind("<<ListboxSelect>>",itemSelected) # binding
lb.pack(pady=5)
root.mainloop()
Output:
The reader should pay attention to line 22, which generates a virtual <<ListboxSelect>>
event when the options in the Listbox are clicked, at which point the itmeChanged( ) method can be triggered to handle this event. Lines 3 to 6 of the program display the selected content in the tabs above. Line 4 to 6 is also a new concept, in line 4 first obtain the event object obj, this example of this object is the Listbox object, and then use this obj object to obtain the selected item index, and then by the index to obtain the selected item. Of course, you can also omit the fourth line, directly using the original Listbox object lb can also be.
from tkinter import *
def itemSelected(event):
index = lb.curselection()
var.set(lb.get(index))
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x250")
var = StringVar()
# var.set("cxq123")
lab = Label(root,text="",textvariable=var,bg="yellow")
lab.pack(pady=5)
lb = Listbox(root)
for fruit in fruits:
lb.insert(END,fruit)
lb.bind("<<ListboxSelect>>",itemSelected)
lb.pack(pady=5)
root.mainloop()
Output:
Early or some people on the network do not understand the concept of virtual binding, in the design of such programs, as a result of clicking is tkinter bound to select the items of the Listbox, it is handled by double-clicking <Double-Button-1>
way, the selected items on the label.
Tkinter ListBox Virtual binding applied to radio selection Example#2
Use <Double-Button-1>
instead of the virtual event <ListboxSelect>
.
from tkinter import *
def itemSelected(event):
obj = event.widget
index = obj.curselection()
print(index)
print(type(obj.get(index)))
print(obj.get(index))
var.set(obj.get(index))
fruits = [
"Banana","Watermelon","Pineapple",
"Orange","Grapes","Mango"
]
root = Tk()
root.title("apidemos.com")
root.geometry("300x250")
var = StringVar()
# var.set("cxq123")
lab = Label(root,text="",textvariable=var)
lab.pack(pady=5)
lb = Listbox(root)
for fruit in fruits:
lb.insert(END,fruit)
lb.bind("<Double-Button-1>",itemSelected)
lb.pack(pady=5)
root.mainloop()
Output:
The purpose of explaining this procedure is to tell the reader that someone before or on the network so that the reader is recommended to use the method of example 1, because from the standpoint of the user, of course, expect to click to select and will be selected to complete the processing of items.