Lambda using python in tkinter

Source: Internet
Author: User

In tkinter, for example, a control is associated with a function, such as a button.CodeAs follows:

Button = button (window, text = 'lowdown ', command = click)

Here, the click function is associated with the button plug-in, but it is difficult to pass parameters. Tkinter requires that controller functions triggered by buttons (or other plug-ins) do not contain parameters, so that they can be called in a unified manner. So how can we pass parameters to the function? This is often used. The answer is to use lambda.

Lambda allows us to create a single-row function without a name anywhere. The following is a simple example.

>>> Lambda: 3

<Function <Lambda> at 0x00a89930>

>>> (Lambda: 3 )()

3

The first expression creates an anonymous function. The function returns the result. 3. The second expression is created and the same function is executed immediately.

Lambda is a function parameter before the colon. Multiple parameters can be separated by commas. The return value is displayed on the right side of the colon.

The following is a Lambda function with parameters.

>>> (Lambda X: 2 * X) (3)

6

This can help us a lot in the GUI, so that our function controller can pass parameters. Take plug-in button as an Example

Button = button (window, text = 'lowdown ', command = lambda: Click (counter, 1 ))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.