Using lambda in GUI programs in Python

Hi, friends in this session we will be learning how to use lambda function in GUI based programs in python.  Most of the modern programs are GUI based that is they have a graphical user interface to interact with the user.  As we know other programs that we see don’t interact much with the user.  Some interact with the user using CLUI that is a command line user interface.  Before learning about lambda let us learn the field in which we use it to play that is graphical user interface.

GUI in Python

GUI based programs communicate with the user and make the program more interactive by placing widgets.  Widgets are any useful tools that make the program more interactive.  Widgets are short for window-gadgets they are called so because all the widgets are enclosed in the main gadget called the window.  As we know that python consists of many modules.  We also have modules in python that consists of some handy tools to create GUI based programs.  Tkinter is one such module that is used to create GUI based applications.  To learn more about GUI applications in python go through this link   GUI using Python

In this session, I am going to teach you the usage of a lambda function in GUI based applications.

Usage of the lambda function

First, let us see a basic GUI program in python that acts as a counter.  We used the Tkinter module to build this application.

def click():
counter.set(counter.get() + 1)
if __name__ == '__main__':
    window = tkinter.Tk()
    counter = tkinter.IntVar()
    counter.set(0)
    frame = tkinter.Frame(window)
    frame.pack()
    button = tkinter.Button(frame, text='Click', command=click)
    button.pack()
    label = tkinter.Label(frame, textvariable=counter)
    label.pack()
    window.mainloop

In the above program we had initially created a window and we placed a button and label into it.  You can see the keyword command in the declaration of the button widget.  It specifies that whenever user clicks the button the action specified by the command keyword has to be executed.  We also gave the action to be executed is the click function which increases the counter value by 1. This program acts as a counter perfectly but there is a room for improvement.  What if we have to both the operations that are down counter and also up counter.  That is we should have two actions to be performed when specified that is one which acts as a normal counter increase by 1.  And the other is down counter which decreases by 1 when the action is triggered.

Improvising using lambda:

Now let us how we can use lambda function for this task.  First of all let us know what a lambda function is

A lambda function allows us to create a one line function anywhere without giving it a name.  But the question is how is it helpful in our GUI applications.  The point where our task gets complicated is that we have to use two different functions for two different counters.  This appears to look easier in this small application but turns complex in large applications. Otherwise, we have to use function with parameters which cannot be directly placed at the command key word.

This is where we take advantage of lambda.  Now see this code with lambda that acts as both up and down counter when we click the respective buttons and the count is displayed on a label.  Look, how much interactive we can make a program with widgets.

import tkinter
window = tkinter.Tk()
counter = tkinter.IntVar()
counter.set(0)
def click(var, value):
    var.set(var.get() + value)
    frame = tkinter.Frame(window)
    frame.pack()
button = tkinter.Button(frame, text='Up', command=lambda: click(counter, 1))
button.pack()
button = tkinter.Button(frame, text='Down', command=lambda: click(counter, -1))
button.pack()
label = tkinter.Label(frame, textvariable=counter)
label.pack()
window.mainloop()

This code creates a no argument lambda function to pass into each button just where it’s needed. The lambda functions then pass the values into click function.

You can learn: Tkinter pack(), grid() Method In Python

This is all about using lambda in GUI programs in Python.  Hope you like this

Also, read:

 

Leave a Reply

Your email address will not be published. Required fields are marked *