Home > Mobile >  pandas deciding code to execute based on user input for Tkinter
pandas deciding code to execute based on user input for Tkinter

Time:07-09

I have a excel data like as below

USA EVENT 2021,

USA - OVERALL REVENUE - DIVISION,FY_1920,FY_2021,FY_2122
CARS,
VEHICLES,
LORRIES,
2-WHEELERS

enter image description here

I would like to replace certain keywords in above excel data. So, I used python xlwings package to replace keywords in excel sheet.

For ex: EVENT 2021 has to be replaced by EVENT 2022. Similarly, user will have to replace a list of keywords. But the problem is how can I dynamically generate the number of code lines to execute?

For ex: If user enters wants only EVENT 2021 to be replaced with EVENT 2022, only below line should be code.

sheet1.used_range.api.Replace("EVENT 2021", "EVENT 2022")

For ex: If user enters wants two words - EVENT 2021, FY_2122 to be replaced with EVENT 2022 and FY_2223 below two line should be executed.

sheet1.used_range.api.Replace("EVENT 2021", "EVENT 2022")
sheet1.used_range.api.Replace("FY_2122", "FY_2223")

For ex: If user enters wants three words - EVENT 2021, FY_2122, FY_2021 to be replaced with EVENT 2022,FY_2223 and FY_2122, below three lines should be executed.

sheet1.used_range.api.Replace("EVENT 2021", "EVENT 2022")
sheet1.used_range.api.Replace("FY_2122", "FY_2223")
sheet1.used_range.api.Replace("FY_2021", "FY_2122")

As you can see, the code lines is same and repeating based on number of keywords to replace.

How can I get an input from user in runtime (for tkinter like GUI) and let pandas decide how many lines of code to run?

How to make this dynamic? Instead of me, manually writing n lines of code for n keywords

CodePudding user response:

To give you an exempel of a tkinter window with two entries and a button that uses the entries content in a called function by the button.

import tkinter as tk

def execute():
    lok = list_of_keywords
    lor = list_of_replacew
    add_keywords() #if user uses submit without add to chart before
    for idx,i in enumerate(list_of_keywords):
        print(f'replace {lok[idx]} with {lor[idx]}')
    lok.clear() #clear list of keywords
    lor.clear() #clear list of replacements
    set_default()

def set_default():
    entry_1.delete(0,'end') #clear entry_1
    entry_2.delete(0,'end') #clear entry_2
    entry_1.focus_set() #focus to entry_1

def add_keywords():
    keyword = entry_1.get()
    replace = entry_2.get()
    if not '' in (keyword,replace): 
        list_of_keywords.append(entry_1.get())
        list_of_replacew.append(entry_2.get())
        set_default()
    else:
        print('nothing to add')

list_of_keywords = []
list_of_replacew = []

root = tk.Tk() #window

label_1 = tk.Label(root,text='Keyword to search:') #User hint
entry_1 = tk.Entry(root) # Entry that takes user input
label_2 = tk.Label(root,text='Replace with:') #User hint
entry_2 = tk.Entry(root) # Entry that takes user input
button_1 = tk.Button(root,text='submit',command=execute) #Button to run function
button_2 = tk.Button(root,text='add to chart',command=add_keywords)
##pack widgets onto the window
button_1.pack(side=tk.BOTTOM,fill=tk.X)
button_2.pack(side=tk.BOTTOM,fill=tk.X)
label_1.pack(side=tk.LEFT)
entry_1.pack(side=tk.LEFT)
label_2.pack(side=tk.LEFT)
entry_2.pack(side=tk.LEFT)
##widget bindings
#use the return key to jump to entry_2
entry_1.bind('<Return>',lambda e:entry_2.focus_set())
#use the return key as option to run function
entry_2.bind('<Return>',lambda e:add_keywords())
###
set_default()

root.mainloop()
  • Related