通过点击按钮

时间:2018-05-27 00:21:58

标签: python tkinter

目前正在学习tkinter,但无法将变量传递给我的点击功能。

我希望每次都将变量x传递给click()和x,然后在新列中进行每次单击,但下面的代码不起作用。

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 个答案:

答案 0 :(得分:1)

使用lamda并将其更改为:

Button(window, text="SUBMIT", width=6, command=lambda: click(x)) .grid(row=3, column 0, sticky=W)
相关问题