通过单击按钮传递变量

2024-04-26 00:22:05 发布

您现在位置:Python中文网/ 问答频道 /正文

目前正在学习tkinter,但在将变量传递给click函数时遇到问题。你知道吗

我想把变量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()

Tags: texttkintercolumnwindowlabelgridrowbg

热门问题