from tkinter import *
def click():
entered_text = entry.get()
output.delete(0.0, END)
try:
definition = my_glossary[entered_text]
except:
definition = "There is no entry for this word."
output.insert(END, definition)
def clickNew():
global my_glossary
addToGlossary = entryNew.get()
addToGlossaryDef = outputNew.get()
my_glossaryOpen = open("glossary.txt", "a")
addToGlossaryTotal = (addToGlossary, ",", addToGlossaryDef)
my_glossaryOpen.write(repr(addToGlossaryTotal))
my_glossaryOpen.close()
def addNewClose():
addNew.destroy()
def addNewWord():
global addNew
global entryNew
global outputNew
addNew = Tk()
addNew.title("Add A New Word")
Label(addNew, text="Enter the word you are defining: ").grid(row=0, column=0, sticky=W)
entryNew = Entry(addNew, width=20, bg="light blue")
entryNew.grid(row=1, column=0, sticky=W)
Button(addNew, text="SUBMIT", width=5, command=clickNew).grid(row=2, column=0, sticky=W)
Button(addNew, text="Close", width=5, command=addNewClose).grid(row=0, column=3, sticky=W)
Label(addNew, text="\nDefinition: ").grid(row=3, column=0, sticky=W)
outputNew = Entry(addNew, width=75, bg="light blue")
outputNew.grid(row=4, column=0, sticky=W)
addNew.bind("<Return>", clickNew)
addNew.mainloop()
window = Tk()
window.title("My Coding Club Glossary")
Label(window, text="Enter the word you want defining: ").grid(row=0, column=0, sticky=W)
entry = Entry(window, width=20, bg="light green")
entry.grid(row=1, column=0, sticky=W)
Button(window, text="SUBMIT", width=5, command=click).grid(row=2, column=0, sticky=W)
Button(window, text="Add New Word", width=13, command=addNewWord).grid(row=0, column=1, sticky=W)
Label(window, text="\nDefinition: ").grid(row=3, column=0, sticky=W)
output = Text(window, width=75, height=6, wrap=WORD, background="light green")
output.grid(row=4, column=0, columnspan=2,sticky=W)
with open('glossary.txt','r') as inf:
my_glossary = eval(inf.read())
window.bind("<Return>", click)
window.mainloop()
please answer the question given at the top...thanks
I need to get my_glossary to get the data from the txt file but when I add the data using the addNewWord function I get errors such as a syntax error inside the txt file saying that the {} or '' are the cause of the issue.
from tkinter import *
def dictest():
global my_glossary
inFile = open("glossary.txt", "r")
inText = inFile.read()
inFile.close()
my_glossary = {}
for line in inText.splitlines():
if line != '':
key,value = line.split(',')
my_glossary[key] = value
my_glossary['newkey'] = 'and the new value'
for k,v in my_glossary.items():
print('key:', k, ', value:', v)
def click():
entered_text = entry.get()
output.delete(0.0, END)
try:
definition = my_glossary[entered_text]
except:
definition = "There is no entry for this word."
output.insert(END, definition)
def clickNew():
global my_glossary
addToGlossary = entryNew.get()
addToGlossaryDef = outputNew.get()
my_glossaryOpen = open("glossary.txt", "a")
addToGlossaryTotal = addToGlossary, addToGlossaryDef,
my_glossaryOpen.write(repr(addToGlossaryTotal))
my_glossaryOpen.close()
def addNewClose():
addNew.destroy()
def addNewWord():
global addNew
global entryNew
global outputNew
addNew = Tk()
addNew.title("Add A New Word")
Label(addNew, text="Enter the word you are defining: ").grid(row=0, column=0, sticky=W)
entryNew = Entry(addNew, width=20, bg="light blue")
entryNew.grid(row=1, column=0, sticky=W)
Button(addNew, text="SUBMIT", width=5, command=clickNew).grid(row=2, column=0, sticky=W)
Button(addNew, text="Close", width=5, command=addNewClose).grid(row=0, column=3, sticky=W)
Label(addNew, text="\nDefinition: ").grid(row=3, column=0, sticky=W)
outputNew = Entry(addNew, width=75, bg="light blue")
outputNew.grid(row=4, column=0, sticky=W)
addNew.bind("<Return>", clickNew)
addNew.mainloop()
dictest()
window = Tk()
window.title("My Coding Club Glossary")
Label(window, text="Enter the word you want defining: ").grid(row=0, column=0, sticky=W)
entry = Entry(window, width=20, bg="light green")
entry.grid(row=1, column=0, sticky=W)
Button(window, text="SUBMIT", width=5, command=click).grid(row=2, column=0, sticky=W)
Button(window, text="Add New Word", width=13, command=addNewWord).grid(row=0, column=1, sticky=W)
Label(window, text="\nDefinition: ").grid(row=3, column=0, sticky=W)
output = Text(window, width=75, height=6, wrap=WORD, background="light green")
output.grid(row=4, column=0, columnspan=2,sticky=W)
f = open("glossary.txt", "r")
num_lines = sum(1 for line in open("glossary.txt"))
def readLines():
global my_glossary
global line
global words
global f
f = open("glossary.txt", "r")
line = f.readline()
words = line.split(",")
my_glossary = {}
my_glossary[words[0]]=words[1]
line = f.readline()
words = line.split(",")
my_glossary[words[0]]=words[1]
while num_lines > 0:
readLines()
num_lines -=1
window.bind("<Return>", click)
window.mainloop()