python中 command=lambda
时间: 2023-10-18 13:04:51 浏览: 62
在Python中,command=lambda是一种将函数作为参数传递的方式,通常用于GUI编程中。具体来说,它可以将一个函数绑定到一个控件上,当该控件被触发时,该函数就会被执行。
例如,以下代码将一个按钮绑定到一个函数上:
```
from tkinter import *
root = Tk()
def my_function():
print("Button clicked")
my_button = Button(root, text="Click me", command=lambda: my_function())
my_button.pack()
root.mainloop()
```
在上面的例子中,我们使用lambda表达式将my_function函数作为参数传递给command选项。当用户点击按钮时,my_function函数会被执行。注意,lambda表达式中的冒号后面是函数调用,而不是函数定义。
相关问题
补充以下代码的self.e()函数:#!/usr/bin/python #coding:utf-8 from tkinter import * import time root = Tk() def cacl(input_str): if "x" in input_str: ret = input_str.split("x") return int(ret[0]) * int(ret[1]) def callback(n): print(n) def callback1(n): print(n) class App: def __init__(self, master): frame1 = Frame(master) frame1.pack() frame = Frame(master) frame.pack() Button(frame, text="1",command=lambda: callback(1) ).grid(row=0,column=0) Button(frame, text="2",command=lambda: callback(2) ).grid(row=0,column=1) Button(frame, text="3",command=lambda: callback(3) ).grid(row=0,column=2) Button(frame, text="4",command=lambda: callback(4) ).grid(row=1,column=0) Button(frame, text="5",command=lambda: callback(5) ).grid(row=1,column=1) Button(frame, text="6",command=lambda: callback(6) ).grid(row=1,column=2) Button(frame, text="7",command=lambda: callback(7) ).grid(row=2,column=0) Button(frame, text="8",command=lambda: callback(8) ).grid(row=2,column=1) Button(frame, text="9",command=lambda: callback(9) ).grid(row=2,column=2) Button(frame, text="0",command=lambda: callback(0) ).grid(row=3,column=0) Button(frame, text="+",command=lambda: callback1("+") ).grid(row=3,column=1) Button(frame, text="-",command=lambda: callback1("-") ).grid(row=3,column=2) Button(frame, text="*",command=lambda: callback1("*") ).grid(row=4,column=1) Button(frame, text="/",command=lambda: callback1("/") ).grid(row=4,column=2) Button(frame, text="=", command=self.say_hi).grid(row=4,column=0) w = Label(frame1,text="输入结果") w.pack() self.e = Entry(frame1) self.e.pack(padx=5) w1 = Label(frame1,text="计算结果") w1.pack() v = StringVar() e1 = Entry(frame1, textvariable=v) v.set("") self.v = v e1.pack() def say_hi(self): print("hi there, everyone!",self.e.get()) input_str = self.e.get() self.v.set(cacl(input_str)) app = App(root) root.mainloop()
您可以尝试将如下代码添加到App类中:
```python
def say_hi(self):
print("hi there, everyone!",self.e.get())
input_str = self.e.get()
self.v.set(cacl(input_str))
```
这是一个名为`say_hi`的方法,它将从输入框中获取文本,将其传递给`cacl`方法进行计算,并将计算结果设置为计算结果文本框的值。
在这个方法中,`self.e.get()`将获取输入框中的文本。然后,它将调用`cacl`方法进行计算,并将计算结果设置为计算结果文本框的值。
最后,你需要将`=`按钮的命令设置为`self.say_hi`,这样当用户单击该按钮时,将调用`say_hi`方法进行计算并更新计算结果文本框的值。在App类的构造函数中,将以下代码添加到按钮的创建行:
```python
Button(frame, text="=", command=self.say_hi).grid(row=4,column=0)
```
完整的代码如下所示:
```python
from tkinter import *
import time
root = Tk()
def cacl(input_str):
if "x" in input_str:
ret = input_str.split("x")
return int(ret[0]) * int(ret[1])
def callback(n):
print(n)
def callback1(n):
print(n)
class App:
def __init__(self, master):
frame1 = Frame(master)
frame1.pack()
frame = Frame(master)
frame.pack()
Button(frame, text="1",command=lambda: callback(1) ).grid(row=0,column=0)
Button(frame, text="2",command=lambda: callback(2) ).grid(row=0,column=1)
Button(frame, text="3",command=lambda: callback(3) ).grid(row=0,column=2)
Button(frame, text="4",command=lambda: callback(4) ).grid(row=1,column=0)
Button(frame, text="5",command=lambda: callback(5) ).grid(row=1,column=1)
Button(frame, text="6",command=lambda: callback(6) ).grid(row=1,column=2)
Button(frame, text="7",command=lambda: callback(7) ).grid(row=2,column=0)
Button(frame, text="8",command=lambda: callback(8) ).grid(row=2,column=1)
Button(frame, text="9",command=lambda: callback(9) ).grid(row=2,column=2)
Button(frame, text="0",command=lambda: callback(0) ).grid(row=3,column=0)
Button(frame, text="+",command=lambda: callback1("+") ).grid(row=3,column=1)
Button(frame, text="-",command=lambda: callback1("-") ).grid(row=3,column=2)
Button(frame, text="*",command=lambda: callback1("*") ).grid(row=4,column=1)
Button(frame, text="/",command=lambda: callback1("/") ).grid(row=4,column=2)
Button(frame, text="=", command=self.say_hi).grid(row=4,column=0)
w = Label(frame1,text="输入结果")
w.pack()
self.e = Entry(frame1)
self.e.pack(padx=5)
w1 = Label(frame1,text="计算结果")
w1.pack()
v = StringVar()
e1 = Entry(frame1, textvariable=v)
v.set("")
self.v = v
e1.pack()
def say_hi(self):
print("hi there, everyone!",self.e.get())
input_str = self.e.get()
self.v.set(cacl(input_str))
app = App(root)
root.mainloop()
```
注意,这个计算器只能计算形如"n x m"的表达式,如果你想要计算其他形式的表达式,需要对`cacl`方法进行修改。
command=lambda
`lambda`是Python中的一个关键字,用于创建匿名函数。在这个特定的代码片段中,`lambda`被用来创建一个匿名函数,它将`button_text`作为参数,并调用`self.button_click(x)`函数。这种方式可以在创建按钮时动态地传递参数,避免了需要在不同的按钮上创建不同的回调函数的麻烦。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)