0

I want to create 10 buttons using a loop which, when pressed, print their their value. The first button prints 0 and the last should print 9.

I found that running the code below, it creates the 10 buttons but when any of them are pressed, it outputs the number 9 only

`

from tkinter import *
import tkinter as tk

root = tk.Tk()
root.geometry("500x500")
def pressed(i):
    print(i)
for i in range(10):
    Button(root, command=lambda:pressed(i)).pack()

root.mainloop()

`

I tried this. I expected for the first button to print 0, the second to print 1 and continue until the last prints 9.

What I got instead was that every button prints 9

0 Answers0