Python Tkinter Map() Function

In this section, we will talk about Python Tkinter Map() function. This blog will tell you how to use the map() in Python Tkinter.

Python Tkinter Map()

The Python Tkinter map() function takes two arguments, first is the function name & the second is the iterables. Iterables are the list of items on which the function is to be applied. Map applies the given function on all the items in the list one by one.

Syntax:

 map(func, *iterables)

Code:

This is the implementation of the map function.

In this code, the user can enter multiple salaries. Every time he enters a salary & clicks on the add button. The salary is added to the list. The user needs to provide the appraisal percentage. Clicking on applying will project the new incremented salary.

from tkinter import *

sal = []

# function

def mapfunction():
    res = (list(map(apply, sal))) 
    items_lb.config(text=f'{res}')

def apply(n):
    percentage = int(hike_tf.get())/100
    n = n + n * percentage
    return n

def clear():
    sal_tf.delete(0, END)
    hike_tf.delete(0, END)
    items_lb.config(text='')
    sal.clear()

def addtolist():
    temp = int(sal_tf.get())
    sal.append(temp)
    items_lb.config(text=f'{sal}')
    sal_tf.delete(0, END)


f = ('Times bold', 14)    

ws = Tk()
ws.title('PythonGuides')
ws.geometry('400x300')
ws.config(bg='#2E7175')

# Frame Widget
frame = Frame(
    ws,
    bg='#2E7175'
    )
frame.place(relx=0.5, rely=0.5, anchor=CENTER )

inside_frame = Frame(
    frame
)
inside_frame.grid(row=1, columnspan=3)


# Label Widget
sal_lb = Label(
    frame, 
    text="Salary",
    font=f,
    bg='#2E7175',
    padx=10
    )
sal_lb.grid(row=0, column=0)

hike_lb = Label(
    frame, 
    text="Hike(%)",
    font=f,
    bg='#2E7175',
    )
hike_lb.grid(row=2, column=0)

items_lb = Label(
    frame,
    text='',
    bg='#2E7175',
    font=f,
    )
items_lb.grid(row=1, column=0, columnspan=3, pady=30)


# Entry Widget
sal_tf = Entry(frame)
sal_tf.grid(row=0, column=1)

hike_tf = Entry(frame)
hike_tf.grid(row=2, column=1)

# Button widget
add_btn = Button(
    frame,
    text="Add",
    padx=10,
    command=addtolist
)
add_btn.grid(row=0, column=2, padx=10)

apply_btn = Button(
    frame, 
    text="Apply", 
    padx=10, 
    pady=5,
    command=mapfunction
    )
apply_btn.grid(row=4, column=0, pady=30)

clear_btn = Button(
    frame, 
    text="Clear", 
    padx=10, 
    pady=5,
    command=clear
    )
clear_btn.grid(row=4, column=1, pady=30)

exit_btn = Button(
    frame, 
    text="Exit", 
    padx=10, 
    pady=5,
    command=lambda:ws.destroy()
    )
exit_btn.grid(row=4, column=2, pady=30)

ws.mainloop()

Output:

In this output, user entered two salaries & provided 5 percent increment on each. The map function took a function created for increment and applies to to these two salaries as you can see in the second picture.

Python Tkinter Map() Function
Python Tkinter Map() Function

You may like the following Python tkinter tutorials:

In this section, we have learned how to use the map() function in Python Tkinter.