Using lambda in GUI programs in Python
Python Lambda Functions are anonymous function means that the function is without a name.
In this article, we will learn to use lambda functions in Tkinter GUI. We use lambda functions to write our code more efficiently and to use one function many times by passing different arguments. We use the lambda function to pass arguments to other functions that are executed when any button is pressed. We can also achieve the same thing by using different functions but that is an inefficient way.
Prerequisite: Tkinter
Procedure to use lambda in GUI programs:
- Define a function that needs to be executed when a button is clicked.
Python3
def greet(value): print (value) |
- Define the button that needs to use this function. Use the lambda function in the command parameter of the button.
Python3
Button(root,text = "Click me to Greet" ,command = lambda : greet( "Hello User" ),bd = 5 ,fg = "blue" ,font = "calibre 18 bold" ).pack() |
Example 1: In this GUI we will have a button that uses the lambda function to call a greet function with a parameter. The greet function prints the Greeting message in GUI.
Python3
from tkinter import * # functions def greet(value): print (value) # update Label text in GUI output_label[ "text" ] = value if __name__ = = "__main__" : root = Tk() root.title( "GFG" ) root.geometry( "800x400" ) # GUI Heading Label(root, text = "Using lambda in GUI programs in Python" , font = "calibre 28 bold" , padx = 5 , pady = 15 ).pack() # Button Button(root, text = "Click me to Greet" , command = lambda : greet( "Hello User" ), bd = 5 , fg = "blue" , font = "calibre 18 bold" ).pack() output_label = Label( root, text = " ", font=" calibre 28 bold", padx = 5 , pady = 15 ) output_label.pack() root.mainloop() |
Output:
Example 2: In this GUI we will have 2 buttons and one entry widget that initially has a value of 0. When button 1 is pressed, the value in the entry widget is increased by 1. And when button 2 is pressed, the value in the entry widget is decreased by 1.
Python3
from tkinter import * # function to update entry widget def update_entry(value): print (value) new_value = output.get() + value output. set (new_value) if __name__ = = "__main__" : root = Tk() root.title( "GFG" ) root.geometry( "800x400" ) # Variable output = IntVar() output. set ( 0 ) # GUI Heading Label(root, text = "Using lambda in GUI programs in Python" , font = "calibre 28 bold" , padx = 5 , pady = 15 ).pack() # Entry Entry(root, textvariable = output, font = "calibre 25 normal" , fg = "blue" , relief = SUNKEN).pack() # Button 1 Button(root, text = "+1" , command = lambda : update_entry( 1 ), bd = 5 , fg = "blue" , font = "calibre 18 bold" , padx = 10 ).pack() # Button 2 Button(root, text = "-1" , command = lambda : update_entry( - 1 ), bd = 5 , fg = "blue" , font = "calibre 18 bold" , padx = 10 ).pack() root.mainloop() |
Output:
Example 3: In this GUI we will have 10 buttons and one entry widget that is initially empty. With these buttons, we will be able to write any number.
Python3
from tkinter import * # function to update entry widget def update_entry(value): print (value) new_value = output.get() + str (value) output. set (new_value) if __name__ = = "__main__" : root = Tk() root.title( "GFG" ) root.geometry( "800x400" ) # Variable output = StringVar() output. set ("") # GUI Heading Label(root,text = "Using lambda in GUI programs in Python" ,font = "calibre 28 bold" ,padx = 5 ,pady = 15 ).pack() # Entry Entry(root,textvariable = output,font = "calibre 25 normal" ,fg = "blue" ,relief = SUNKEN).pack() # Buttons Button(root,text = "0" ,command = lambda : update_entry( 0 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) Button(root,text = "1" ,command = lambda : update_entry( 1 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) Button(root,text = "2" ,command = lambda : update_entry( 2 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) Button(root,text = "3" ,command = lambda : update_entry( 3 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) Button(root,text = "4" ,command = lambda : update_entry( 4 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) Button(root,text = "5" ,command = lambda : update_entry( 5 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) Button(root,text = "6" ,command = lambda : update_entry( 6 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) Button(root,text = "7" ,command = lambda : update_entry( 7 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) Button(root,text = "8" ,command = lambda : update_entry( 8 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) Button(root,text = "9" ,command = lambda : update_entry( 9 ),bd = 5 ,fg = "blue" , font = "calibre 18 bold" ,padx = 20 ).pack(side = LEFT) root.mainloop() |