Skip to content

Instantly share code, notes, and snippets.

@slapec
Created August 8, 2023 08:06
Show Gist options
  • Save slapec/099814eaf9be231207eaa0ad99c7ad00 to your computer and use it in GitHub Desktop.
Save slapec/099814eaf9be231207eaa0ad99c7ad00 to your computer and use it in GitHub Desktop.
tkinter_late_binding
from functools import partial
import tkinter as tk
class GameTable(tk.Tk):
def __init__(self):
super().__init__()
for i in range(4):
label = tk.Label(self, text=f'Lap {i + 1}')
label.pack(padx=1, pady=10, side='left')
# Create property dynamically
label.i_value = i
# Prefer this
label.bind('<Button-1>', partial(self.gets_clicked, i))
# Over this
label.bind('<Button-1>', lambda e, i_inner=i: self.gets_clicked(i_inner, e))
def gets_clicked(self, i, event):
print(i, event.widget.i_value)
if __name__ == '__main__':
gt = GameTable()
gt.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment