- Tkinter GUI Application Development Blueprints(Second Edition)
- Bhaskar Chaudhary
- 156字
- 2021-06-24 18:35:04
Passing arguments to callbacks
If a callback does not take any argument, it can be handled with a simple function, such as the one shown in the preceding code. However, if a callback needs to take arguments, we can use the lambda function, as shown in the following code snippet:
def my_callback (argument)
#do something with argument
Then, somewhere else in the code, we define a button with a command callback that takes some arguments, as follows:
tk.Button(root,text="Click", command=lambda: my_callback ('some argument'))
Python borrows a specific syntax from functional programming, called the lambda function. The lambda function lets you define a single-line, nameless function on the fly.
The format for using lambda is as follows:
lambda arg: #do something with arg in a single line
Here's an example:
square = lambda x: x**2
Now, we can call the square method, as follows:
>> print(square(5)) ## prints 25 to the console
推薦閱讀
- Python爬蟲開發與項目實戰
- Java EE 8 Application Development
- 實戰Java高并發程序設計(第2版)
- Instant Zurb Foundation 4
- 深度實踐KVM:核心技術、管理運維、性能優化與項目實施
- Android高級開發實戰:UI、NDK與安全
- Java高手是怎樣煉成的:原理、方法與實踐
- 算法超簡單:趣味游戲帶你輕松入門與實踐
- 區塊鏈:技術與場景
- BackTrack 5 Cookbook
- Swift編程實戰:iOS應用開發實例及完整解決方案
- Python Natural Language Processing
- Jenkins 2.x Continuous Integration Cookbook(Third Edition)
- Vue.js項目開發實戰
- JavaScript+jQuery交互式Web前端開發(第2版)