0

For the following piece of code, I created three entries, which initially will show [0],[1],[2],[3] respectively. If I press 'Enter' at each entry, the console should print 0,1,2,3. However, all the widgets print 3. What's the problem?

import tkinter as tk

root = tk.Tk()

def show(entryVar):
    print(entryVar.get())

rootEntry = [0 for x in range(4)]
rootEntryvar = [tk.IntVar(value = x) for x in range(4)]
for i in range(4):
    rootEntry[i] = tk.Entry(root, textvariable = rootEntryvar[i], width = 5)
    rootEntry[i].grid(row = 0, column = i)

for i in range(4):
    rootEntry[i].bind('<Return>', lambda a: show(rootEntryvar[i]))

root.mainloop()
Mayan
  • 492
  • 4
  • 11
  • 2
    It should be `lambda a,i=i: show(rootEntryvar[i])`. Read [Tkinter assign button command in loop with lambda](https://stackoverflow.com/questions/17677649/tkinter-assign-button-command-in-loop-with-lambda) – Henry Yik Aug 22 '19 at 07:17
  • Thanks! I didn't aware of the problem of i – Mayan Aug 22 '19 at 07:33

0 Answers0