-1

Currently learning tkinter but having trouble passing a variable to my click function.

I want to pass variable x to click() and x increment each time and make each click in a new column, but below code is not working.

from tkinter import *

def click(x):
    entered_text = textentry.get()  
    Label(window, text=entered_text, bg="black", fg="white") . grid(row=3, 
    column=x, sticky =W)
    x += 1

window = Tk()
window.title("Testing 123")
window.configure(background="black")
x = 1

photo1 = PhotoImage(file="123.png")
Label (window, image=photo1, bg="black") .grid(row=0, column=0, sticky=W)

Label (window, text="Label:", bg="black", fg="white", font="none 12 bold") 
.grid(row=1, column=0, sticky=W)

textentry = Entry(window, width=20, bg="white")
textentry.grid(row=2,column=0,sticky=W)

Button(window, text="SUBMIT", width=6, command=click(x)) .grid(row=3, column = 
0, sticky=W)

window.mainloop()

1 Answers1

1

Use lamda and change it to:

Button(window, text="SUBMIT", width=6, command=lambda: click(x)) .grid(row=3, column 0, sticky=W)
DavidPH
  • 422
  • 1
  • 4
  • 17