Tkinter Button Lambda Expression
In the Tkinter Button Tutorial design process, the Yellow button and the Blue button perform the same job, but the In fact, this is a good time to use Lambda expressions to simplify the design by calling the same method but passing different parameters.
Tkinter Button Lambda Expression Example#1
Using Lambda expressions –
from tkinter import *
def bColor(bgColor):
root.config(bg=bgColor)
root = Tk()
root.title("apidemos.com")
root.geometry("300x200")
# Create three new buttons
exitbtn = Button(root,text="Exit",command=root.destroy)
bluebtn = Button(root,text="Blue",command=lambda : bColor("blue"))
yellowbtn = Button(root,text="Yellow",command=lambda : bColor("yellow"))
# Position the three button packs at the bottom right
exitbtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)
bluebtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)
yellowbtn.pack(anchor=S,side=RIGHT,padx=5,pady=5)
root.mainloop()
Output: