I\'m trying to build a GUI for my python application (interactive dictionary), t
ID: 3706318 • Letter: I
Question
I'm trying to build a GUI for my python application (interactive dictionary), the application already connect with database(JSON) the idea
get the input (word) from the user and then print out the meaning of the word.
My problem I'm trying to figure out how to involve Tkinter and connect it with JSON
import json #import the JSON module
from difflib import get_close_matches #difflib module provides classes and functions
#for comparing sequences
#get_close_matches Return a list of
#the best “good enough” matches
data = json.load(open("data.json")) #load JSON to python dictionary
def translate(w):
w = w.lower() #change the input to lower case
if w in data: #first scenario check if the word exist in the dictionary, if exist load the data
return data[w]
elif w.title() in data: #When the user inputs a proper noun
return data[w.title()] #returns the definition of names that start with a capital letter
elif w.upper() in data: #definition of acronyms
return data[w.upper()]
elif len(get_close_matches(w, data.keys())) > 0: #second scenario compare the word and get the best match
#ask the user if the result of matching what is looking for
YN = input("Did you mean %s instead? Enter y if yes or n if no:" % get_close_matches(w, data.keys())[0])
if YN == "y":
return data[get_close_matches(w, data.keys())[0]]
elif YN == "n":
return "The word doesn't exist. Please double check it."
else:
return "We didn't understand your entry."
#third scenario the word not match or can't found
else:
return "The word doesn't exsit. Please double check it."
word = input("Enter word: ")
#in some cases the word have more than one definition so we need to make the output more readable
output = translate(word)
if type(output) == list:
for item in output:
print(item)
else:
print(output)
Explanation / Answer
//The below program get a short demo regarding the usage of the TKInter class with Json,to start off with this can be //helpful in understanding the basic concept.
from Tkconstants import W, E, N, S
from Tkinter import Tk, IntVar, StringVar
import Tkinter
import json
class TkJson(Tk):
"""
Simple class which wraps Tk and uses some JSON to contruct a GUI.
"""
menu = None
widgets = {}
def __init__(self, filename, title='Tk'):
"""
Initialize a Tk root and created the UI from a JSON file.
Returns the Tk root.
"""
Tk.__init__(self)
self.title(title)
ui_file = open(filename)
user_interface = json.load(ui_file)
self.create_widgets(self, user_interface)
def create_widgets(self, parent, items):
"""
Creates a set of Tk widgets.
"""
for name in items.keys():
current = items[name]
if isinstance(current,
dict) and not self._contains_list(
current) and not self._contains_dict(current):
self._create_widget(name, parent, current)
elif isinstance(current, dict) and self._contains_list(current):
widget = self._create_widget(name, parent, current)
self.create_widgets(widget, current)
elif isinstance(current, dict) and self._contains_dict(current):
widget = self._create_widget(name, parent, current)
self.create_widgets(widget, current)
elif isinstance(current, list):
for item in current:
self.create_widgets(parent, {name: item})