0

I am trying to create a GUI in Python that searches a directory and brings back matches. However, whenever I run it I get the error 'NoneType' object has no attribute 'insert' for the textfield insert part. What am I doing wrong?

from tkinter import *
root = Tk()
root.title("ICD-10-CM Lookup")
ICD_10_CM = StringVar()
label1 = Label(root, text="ICD-10_CM Code").grid(row=0, column=0)
query = Entry(root, textvariable=ICD_10_CM).grid(row=0, column=1)

def search():
    infile = open("icd10cm.txt", "r")
    for line in infile:
       line = line.strip()
       elements = line.split("\t")
       if str(query) in elements[0]:
           textfield.insert(elements[1])
       elif str(query) in elements[1]:
           textfield.insert(elements[0])
    infile.close()

search_button = Button(root, text="Search",   command=search).grid(row=2,column=0)
textfield = Text(root).grid(row=1,column=1,rowspan=2)
root.mainloop()
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
Oliver12
  • 11
  • 1
  • 1

1 Answers1

0

Your textfield variable is None because grid method returns None, so you need to separate initialization and grid method. Same thing with query. Also, insert method requires two args: index and chars.

from tkinter import *
root = Tk()
root.title("ICD-10-CM Lookup")
ICD_10_CM = StringVar()
label1 = Label(root, text="ICD-10_CM Code").grid(row=0, column=0)
query = Entry(root, textvariable=ICD_10_CM)
query.grid(row=0, column=1)


def search():
    infile = open("icd10cm.txt", "r")
    for line in infile:
        line = line.strip()
        elements = line.split("\t")
        if str(query.get()) in elements[0]:
            textfield.insert(INSERT, elements[1])
        elif str(query.get()) in elements[1]:
            textfield.insert(INSERT, elements[0])
        textfield.insert(END, '\n')
    infile.close()


search_button = Button(root, text="Search", command=search).grid(row=2, column=0)
textfield = Text(root)
textfield.grid(row=1, column=1, rowspan=2)
root.mainloop()
  • Thanks, that makes sense. My goal is to run it on a database like:A01.00 Typhoid fever, unspecified A01.01 Typhoid meningitis A01.02 Typhoid fever with heart involvement A01.03 Typhoid pneumonia A01.04 Typhoid arthritis A01.05 Typhoid osteomyelitis . So when I type A01 into the entry field it will bring back all entries with A01. However when I ran it brought back something completely different. When I changed the search term to Typhoid it brought back the same results as before. Is the code not searching line by line looking for the query term and bringing back matches to it? – Oliver12 Dec 14 '17 at 18:21
  • @Oliver12 I've updated answer. Again, query.grid returns None and str(query) will return None. Also you need to use query.get(), not just str(query). – Maksim Sunduk Dec 14 '17 at 19:30