Как можно получить номер выбранного элемента из tkinter.Listbox()
?
from tkinter import *
class ListBox:
def __init__(self):
self.mainwindow = Tk()
self.listResult()
self.mainwindow.mainloop()
def listGet(self,event):
widgetEvent = event.widget
appSelected = widgetEvent.curselection()
if ( len(appSelected) == 1 ):
selected = widgetEvent.get(appSelected[0])
print(selected)
def listResult(self):
self.appsListBox = Listbox(self.mainwindow)
self.appsListBox.insert(END, *("App 1","App 2","App 3"))
for i in range(0, self.appsListBox.size()):
if ( self.appsListBox.get(i)[0] == "-" ):
self.appsListBox.itemconfig(i,
foreground="gray", \
selectforeground="white", \
selectbackground=self.appsListBox.itemcget(i,"background"))
self.appsListBox.pack()
self.appsListBox.bind("<<ListboxSelect>>",self.listGet)
ListBox()