I am trying to get the word entered in the field(exp_textbox) to be passed as an arguement to the function(definition). This is the code below for the dictionary app. the json file is a python dictionary of words and their meaning. Is there a way i could do this?
import tkinter
import json
import difflib
data = json.load(open('data.json'))
main_window = tkinter.Tk()
def definition(w):
w = w.lower()
match_word = difflib.get_close_matches(w, data.keys())
if w in data:
result = data[w]
return result
elif len(match_word) > 0:
answer = input(f"Did you mean '{match_word[0]}'? [Y/N]").upper()
if answer == 'Y':
result = data[match_word[0]]
return result
elif answer == 'N':
return f"'{w}' is not in my dictionary"
else:
return 'command not understood'
else:
return f"'{w}' is not in my dictionary"
# Window Title
main_window.title('Dictionary')
main_window.geometry('320x320')
# Expression Frame
exp_frame = tkinter.Frame(main_window)
exp_frame.grid(row=0, column=0, columnspan=3, sticky='new')
exp_label = tkinter.Label(exp_frame, text='Enter your Word Here: ')
exp_label.grid(row=0, column=0)
The word provided in the field below should be passed to the function(definition) as an arguement.
exp_textbox = tkinter.Entry(master=exp_frame)
exp_textbox.grid(row=0, column=1)
exp_label = tkinter.Button(exp_frame, text='Search', command=definition(exp_textbox.get()))
exp_label.grid(row=0, column=3)
# Definition Frame
def_frame = tkinter.Frame(main_window)
def_frame.grid(row=2, column=0, columnspan=3, sticky='new')
def_label = tkinter.Label(def_frame, text='Definition is : ')
def_label.grid(row=2, column=0)
The return value of the function would then be returned here for the user.
def_textbox = tkinter.StringVar()
tkinter.Entry(def_frame, textvariable=def_textbox).grid(row=2, column=1)
main_window.mainloop()