Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Lambda parameter in for loop takes a <ButtonPress event state=Mod num=1 x= y=> value

Context Menu should show filter variables dynamically and execute a lambda function with parameters defined inside the callback.

import tkinter as tk

...

def get_text():

...
    for i in range(len(obj_list)):
        guidelines = tk.Label(window, text="{0} : {1} ore".format(obj_list[i].name,obj_list[i].nr_ore), fg="blue", cursor="hand2")
        guidelines.grid(row=7+i, column=0, sticky='nsew')
        guidelines.bind("<Button-1>", lambda i=i: show_guide_lines(i))
...
def show_guide_lines(i):
    window_guide_lines = tk.Toplevel(window)
    window_guide_lines.title('Guide Lines')
    window_guide_lines.geometry('400x200')
    print(i)
    guide_line_text = str(i)
    tk.Label(window_guide_lines, text=guide_line_text).pack()

    window_guide_lines.tkraise()
    window_guide_lines.focus_force()
...
window=tk.Tk()
window.title('Calculare numar de ore')
window.eval('tk::PlaceWindow . center')

frame=tk.Frame(window, width=500, height=10)
frame.grid(row=0,column=0)

...

window.mainloop() 

The problem is that the i parameter in the show_guide_lines function takes the value when he should be a number and i don’t know why. Thank you in advance
enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

As the first argument of the callback of an event binding is the Event object, so the lambda definition should be as below:

guidelines.bind("<Button-1>", lambda e, i=i: show_guide_lines(i))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading