Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Python
from tkinter import *

# key press funciton:
def click():
    entered_text = entry.get() # collect text from the text entry box
    output.delete(0.0, END) # clear text box
    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 
        #}
    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")
    
    # create label
    Label(addNew, text="Enter the word you are defining: ").grid(row=0, column=0, sticky=W)

    # create text entry box
    entryNew = Entry(addNew, width=20, bg="light blue")
    entryNew.grid(row=1, column=0, sticky=W)

    # Add a submit button
    Button(addNew, text="SUBMIT", width=5, command=clickNew).grid(row=2, column=0, sticky=W)

    # Add another button
    Button(addNew, text="Close", width=5, command=addNewClose).grid(row=0, column=3, sticky=W)

    # create another label
    Label(addNew, text="\nDefinition: ").grid(row=3, column=0, sticky=W)

    # create text entry box
    outputNew = Entry(addNew, width=75, bg="light blue")
    outputNew.grid(row=4, column=0, sticky=W)

    addNew.bind("<Return>", clickNew)
    
    addNew.mainloop()

#### main
window = Tk()
window.title("My Coding Club Glossary")

# create label
Label(window, text="Enter the word you want defining: ").grid(row=0, column=0, sticky=W)

# create text entry box
entry = Entry(window, width=20, bg="light green")
entry.grid(row=1, column=0, sticky=W)

# Add a submit button
Button(window, text="SUBMIT", width=5, command=click).grid(row=2, column=0, sticky=W)

# Add another button
Button(window, text="Add New Word", width=13, command=addNewWord).grid(row=0, column=1, sticky=W)

# create another label
Label(window, text="\nDefinition: ").grid(row=3, column=0, sticky=W)

# create text box
output = Text(window, width=75, height=6, wrap=WORD, background="light green")
output.grid(row=4, column=0, columnspan=2,sticky=W)

#The dictionary:
#my_glossaryOpen = open("glossary.txt", "r")
#my_glossary = my_glossaryOpen.read()
#my_glossaryOpen.close()
with open('glossary.txt','r') as inf:
    my_glossary = eval(inf.read())


## Binging the enter key
window.bind("<Return>", click)

#### Run mainloop
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.

Python
from tkinter import *

def dictest():
    global my_glossary
# read all lines of the file
    inFile = open("glossary.txt", "r")
    inText = inFile.read()
    inFile.close()
 
    my_glossary = {}
# iterate through all lines, after removing the line-end character(s)
    for line in inText.splitlines():
        if line != '':           # ignore empty lines
            key,value = line.split(',')
            my_glossary[key] = value
 
    my_glossary['newkey'] = 'and the new value'
 
# list all the dictionary entries
    for k,v in my_glossary.items():
        print('key:', k, ', value:', v)

# key press funciton:
def click():
    entered_text = entry.get() # collect text from the text entry box
    output.delete(0.0, END) # clear text box
    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 
        #}
    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")
    
    # create label
    Label(addNew, text="Enter the word you are defining: ").grid(row=0, column=0, sticky=W)

    # create text entry box
    entryNew = Entry(addNew, width=20, bg="light blue")
    entryNew.grid(row=1, column=0, sticky=W)

    # Add a submit button
    Button(addNew, text="SUBMIT", width=5, command=clickNew).grid(row=2, column=0, sticky=W)

    # Add another button
    Button(addNew, text="Close", width=5, command=addNewClose).grid(row=0, column=3, sticky=W)

    # create another label
    Label(addNew, text="\nDefinition: ").grid(row=3, column=0, sticky=W)

    # create text entry box
    outputNew = Entry(addNew, width=75, bg="light blue")
    outputNew.grid(row=4, column=0, sticky=W)

    addNew.bind("<Return>", clickNew)
    
    addNew.mainloop()

#### main
dictest()
window = Tk()
window.title("My Coding Club Glossary")

# create label
Label(window, text="Enter the word you want defining: ").grid(row=0, column=0, sticky=W)

# create text entry box
entry = Entry(window, width=20, bg="light green")
entry.grid(row=1, column=0, sticky=W)

# Add a submit button
Button(window, text="SUBMIT", width=5, command=click).grid(row=2, column=0, sticky=W)

# Add another button
Button(window, text="Add New Word", width=13, command=addNewWord).grid(row=0, column=1, sticky=W)

# create another label
Label(window, text="\nDefinition: ").grid(row=3, column=0, sticky=W)

# create text box
output = Text(window, width=75, height=6, wrap=WORD, background="light green")
output.grid(row=4, column=0, columnspan=2,sticky=W)

#The dictionary:
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

## Binging the enter key
window.bind("<Return>", click)

#### Run mainloop
window.mainloop()
Posted
Updated 29-Jan-15 6:42am
v4
Comments
Richard MacCutchan 28-Jan-15 15:13pm    
Please explain your problem in proper detail. We cannot guess what is in your mind.
Dylan Forsyth 28-Jan-15 15:15pm    
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....sorry for not being clear enough
Richard MacCutchan 28-Jan-15 15:30pm    
You need to show which line of code causes the error, and provide the exact error message. Please don't expect us to be able to make guesses.
Dylan Forsyth 28-Jan-15 15:37pm    
My new problem is the fact that the code wont read the txt file information as a dictionary so returns "There is no entry for this word." instead of the definition.....the contents of the text file are :

"test", "this is another test",

exactly like that^
Dylan Forsyth 28-Jan-15 15:20pm    
changed the code but I cant seem to generate the dictionary from the code

See the following for reading the text file and creating a dictionary. Note you do not need quotes around your text in the glossary file. My file looks like:
foo,something silly
bar,something sillier

Code
Python
f = open('glossary.txt','r')
line = f.readline()
words = line.split(',')
dict = {}
dict[words[0]]=words[1]
line = f.readline()
words = line.split(',')
dict[words[0]]=words[1]

dict    # show the dictionary content

# output
{'foo': 'something silly\n', 'bar': 'something sillier\n'}
 
Share this answer
 
Python
def dictest():
# read all lines of the file
    inFile = open("glossary.txt", "r")
    inText = inFile.read()
    inFile.close()

    glossary = {}
# iterate through all lines, after removing the line-end character(s)
    for line in inText.splitlines():
        if line != '':           # ignore empty lines
            key,value = line.split(',')
            glossary[key] = value

    glossary['newkey'] = 'and the new value'

# list all the dictionary entries
    for k,v in glossary.items():
        print('key:', k, ', value:', v)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900