0

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()
Gavin
  • 1,070
  • 18
  • 24
Caleb
  • 9
  • 2
  • Does this answer your question? [how-to-pass-arguments-to-a-button-command-in-tkinter](https://stackoverflow.com/questions/6920302) – stovfl May 22 '20 at 10:35
  • 1
    Please try to add a meaningful title to this question. The current title is meaningless. – Bryan Oakley May 22 '20 at 23:24

1 Answers1

1

So there are a couple of problems evident.

Running the function on button press

The first issue is that when you are creating the button you immediately run the definition function, instead of only running it when the button is pressed. How to pass arguments to a Button command in Tkinter? has some solutions to this issue, but a simple addition would be:

exp_label = tkinter.Button(exp_frame, text='Search', command=lambda: definition(exp_textbox.get()))

Getting the result of running the function

The next issue is that you are returning the result of checking the definition to the point of call (to the Button object) which does not know what to do with this. A quick way to work around this would be to set the value in the textbox directly in the function itself:

  1. Move the definition of your def_textbox Stringvar above the button
  2. Pass the def_textbox to your button command
  3. Instead of returning the result, use set to change the textbox

Giving final code like:

import tkinter
import json
import difflib

data = json.load(open('data.json'))

main_window = tkinter.Tk()

def definition(w, txtbox):
    print(f'In definition with {w}')
    w = w.lower()
    match_word = difflib.get_close_matches(w, data.keys())
    if w in data:
        result = data[w]
        txtbox.set(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]]
            txtbox.set(result)
        elif answer == 'N':
            txtbox.set(f"'{w}' is not in my dictionary")
        else:
            txtbox.set('command not understood')
    else:
        txtbox.set(f"'{w}' is not in my dictionary")


# Window Title
main_window.title('Dictionary')
main_window.geometry('320x320')

def_textbox = tkinter.StringVar()
# 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)

exp_textbox = tkinter.Entry(master=exp_frame)
exp_textbox.grid(row=0, column=1)
exp_label = tkinter.Button(exp_frame, text='Search', command=lambda: definition(exp_textbox.get(), def_textbox))
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)

tkinter.Entry(def_frame, textvariable=def_textbox).grid(row=2, column=1)
main_window.mainloop()
Gavin
  • 1,070
  • 18
  • 24