Hi have this loop but insted of send the correct value in the button it always send the original value
this creates the buttons named from 0 to 9, but when i run the button it always prints 8 insted of 8,9,10 etc...

from tkinter import *
from tkinter import ttk

root = Tk()

global gen_reprow,x
gen_reprow=8
x=0

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))

def printd(gena):
    print(gena)


def ger_but():
    for x in range(10):
       ttk.Button(mainframe, width="15", text=x,command=lambda gen=gen_reprow: printd(gen)).grid(column=0, row=x, padx=1, pady=15)
        x += 1
        gen_reprow += 1



ger_but()
if __name__ == "__main__":
    root.mainloop()

cant edit :(. this is the correct example

from tkinter import ttk, Tk

root = Tk()

global gen_reprow,x
gen_reprow=8
x=0

mainframe = ttk.Frame(root)
mainframe.grid(column=0, row=0)

def printd(gena):
    print(gena)


def ger_but(vv):
    for x in range(vv):
        ttk.Button(mainframe, width="15", text=x, command=lambda gen=gen_reprow: printd(gen)).grid(column=0, row=x, padx=1, pady=15)
        x += 1
        gen_reprow += 1



ger_but(9) # this needs to print 8 to 15
ger_but(5) # this needs to print 16 to 19

if __name__ == "__main__":
    root.mainloop()
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.