Tkinter List

I want to execute a function with one click on the list. This is my idea:

from Tkinter import * import Tkinter def immediately(): print Lb1.curselection() top = Tk() Lb1 = Listbox(top) Lb1.insert(1, "Python") Lb1.insert(2, "Perl") Lb1.insert(3, "C") Lb1.insert(4, "PHP") Lb1.insert(5, "JSP") Lb1.insert(6, "Ruby") Lb1.pack() Lb1.bind('<Button-1>', lambda event :immediately() ) top.mainloop() 

But this function prints before making a choice ... You will see what is the problem when running this code.

+4
source share
1 answer

You can bind to the <<ListboxSelect>> event as described in this post: Getting a callback when changing the selection of the Tkinter list? TKinter is somewhat strange in that the information does not seem to be contained in the event that is sent to the handler. Also note: there is no need to create a lambda that just calls your function immediately , the function object can be passed as shown:

 from Tkinter import * import Tkinter def immediately(e): print Lb1.curselection() top = Tk() Lb1 = Listbox(top) Lb1.insert(1, "Python") Lb1.insert(2, "Perl") Lb1.insert(3, "C") Lb1.insert(4, "PHP") Lb1.insert(5, "JSP") Lb1.insert(6, "Ruby") Lb1.pack() Lb1.bind('<<ListboxSelect>>', immediately) top.mainloop() 
+8
source

Source: https://habr.com/ru/post/1388181/


All Articles