【问题标题】:Passing a variable through click button [duplicate]通过单击按钮传递变量[重复]
【发布时间】:2018-11-05 23:09:41
【问题描述】:

目前正在学习 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()

【问题讨论】:

    标签: python tkinter


    【解决方案1】:

    使用lamda并将其更改为:

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

    【讨论】:

      猜你喜欢
      • 2020-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-23
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      相关资源
      最近更新 更多