本文介绍了在 tkinter 中单击按钮后返回按钮文本的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个使用此 lambda 函数单击的按钮列表:
I'm trying to create a list of buttons that are clicked with this lambda function:
button1.config(command=(lambda x: (clicked.append(x)))(button1.cget("text")))它似乎有点工作,但它会立即打印按钮文本,即它不会等待用户单击按钮.
It seems to sort of work but it prints the button text immediately i.e. it doesn't wait for user to click the button.
关于如何让它响应按钮点击的任何想法?
Any ideas on how to make it responsive to the button click?
class GraphicsInterface: def __init__(self): self.window = Tk() self.window.geometry("720x500") clicked=[] button1 = Button(self.window, text="Dice 1", width=13) button1.place(x=60, y=160) button1.config(command=(lambda x: (clicked.append(x)))(button1.cget("text"))) print(clicked) 推荐答案试图在 lambda 中完成所有这些是错误的方法.这简直太令人困惑了,如果不是不可能做你想做的事.相反,创建一个完成工作的方法,并仅使用 lambda 作为调用该函数的一种方式:
Trying to do all this in a lambda is the wrong approach. It's simply too confusing, if not impossible to do what you want. Instead, create a method that does the work, and use lambda only as a way to call that function:
from Tkinter import * class GraphicsInterface: def __init__(self): self.window = Tk() self.window.geometry("720x500") self.clicked=[] button1 = Button(self.window, text="Dice 1", width=13) button2 = Button(self.window, text="Dice 2", width=13) button1.pack() button2.pack() button1.configure(command=lambda btn=button1: self.OnClick(btn)) button2.configure(command=lambda btn=button2: self.OnClick(btn)) self.window.mainloop() def OnClick(self, btn): text = btn.cget("text") self.clicked.append(text) print "clicked:", self.clicked app = GraphicsInterface()更多推荐
在 tkinter 中单击按钮后返回按钮文本的方法
发布评论