在 tkinter 中单击按钮后返回按钮文本的方法

编程入门 行业动态 更新时间:2024-10-28 12:16:52
本文介绍了在 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 中单击按钮后返回按钮文本的方法

本文发布于:2023-08-06 05:06:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310248.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:按钮   单击   文本   方法   tkinter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!