Python Tkinter button callback -
i trying print out button number when click each buttons created loop. following have tried.
import tkinter tk root=tk.tk() def myfunction(a): print in range(10): tk.button(root,text='button'+str(i),command=lambda:myfunction(i)).place(x=10,y=(10+(25*i))) root.mainloop()
but instead of printing out each button number, giving me last button number everytime. there can when click button 1, print 1,2 2 ,and on?
blender's answer clever solution in case thrown off function abstraction, here possible way it. creates mapping, saved in buttons
, button
widgets proper numbers.
import tkinter tk root = tk.tk() def myfunction(event): print buttons[event.widget] buttons = {} in range(10): b = tk.button(root, text='button' + str(i)) buttons[b] = # save button, index key-value pair b.bind("<button-1>", myfunction) b.place(x=10,y=(10+(25*i))) root.mainloop()
Comments
Post a Comment