I'm a Python newbie and I made this GUI dictionary using PyDictionary module, it took me long though, I can sense that the if conditions are really unpractical and ugly, but I couldn't find a way to shorten that part of the code, any recommendations will be highly appreciated.
from tkinter import *
from PyDictionary import PyDictionary
root = Tk()
root.geometry("450x550")
root.columnconfigure(0, weight=1)
root.config(bg="black")
def space():
space = Label(text="", bg="black")
space.grid()
def find_meaning():
word = entry.get()
dictionary = PyDictionary(word)
definition = dictionary.getMeanings()
print(definition)
label = ""
try:
if "Verb" in definition[word]:
label = "Verb: " + definition[word]["Verb"][0]
if "Noun" in definition[word]:
label = "Noun: " + definition[word]["Noun"][0]
if "Adverb" in definition[word]:
label = "Adverb: " + definition[word]["Adverb"][0]
if "Noun" in definition[word] and "Adverb" in definition[word]:
label = "Noun: " + definition[word]["Noun"][0] + " \n\n " + "Adverb: " + definition[word]["Adverb"][0]
if "Noun" in definition[word] and "Adjective" in definition[word] and "Adverb" in definition[word]:
label = "Noun: " + definition[word]["Noun"][0] + " \n\n " + "Adjective: " + definition[word]["Adjective"][0] + " \n\n " + "Adverb: " + definition[word]["Adverb"][0]
if "Adjective" in definition[word]:
label = "Adjective: " + definition[word]["Adjective"][0]
if "Noun" in definition[word] and "Adjective" in definition[word] and "Adverb" in definition[word] and "Verb" in definition[word]:
label = "Noun: " + definition[word]["Noun"][0] + " \n\n " + "Adjective: " + definition[word]["Adjective"][
0] + " \n\n " + "Adverb: " + definition[word]["Adverb"][0] + " \n\n " + "Verb: " + definition[word]["Verb"][0]
if "Verb" in definition[word] and "Noun" in definition[word]:
label = "Noun: " + definition[word]["Noun"][0] + " \n\n " + "Verb: " + definition[word]["Verb"][0]
if "Noun" in definition[word] and "Adjective" in definition[word]:
label = "Noun: " + definition[word]["Noun"][0] + " \n\n " + "Adjective: " + definition[word]["Adjective"][0]
if "Adjective" in definition[word] and "Verb" in definition[word]:
label = "Adjective: " + definition[word]["Adjective"][0] + " \n\n " + "Verb: " + definition[word]["Verb"][0]
except Exception:
label = "Wrong word entered!"
return label
def write():
label.config(text=find_meaning())
entry.delete(0, END)
space()
dic_text = Label(root, text="Dictionary", fg="#3dcc8e", bg="black", font=("arial", 15, "bold"))
dic_text.grid()
space()
entry = Entry(root, font=("times", 15, "bold"))
entry.grid()
space()
btn = Button(root, text="Find Meaning", command=lambda: [find_meaning(), write()])
btn.grid()
space()
label = Label(root, text="Translation", background="#3e3e3e",
width=40, height=21, relief=FLAT, state=DISABLED, disabledforeground="#3dcc8e", wraplength=200,
justify=LEFT)
label.grid()
root.mainloop()