Home > Mobile >  Search the data on the text file and Printing in GUI with Tkinter
Search the data on the text file and Printing in GUI with Tkinter

Time:07-26

I am writing a python program to search the data on the text file in GUI

The search function normally gives the result (in CLI). I want to use it with Tkinter, but when I pull the input with the Tkinter Entry function, my search function does not work.

Whatever I write, it outputs the data in the entire text file. I think the problem is in the if msg.get() in line:

The search function is below.

def search():
    with open(r"loglar.txt", 'r') as fp:
        for l_no, line in enumerate(fp):
            lineNum = l_no   1
            # search string
            if msg.get() in line:
                lineNumber = ('Line Number:', lineNum)
                lineWord = ('Line:', line)
                print(lineNumber)
                print(lineWord)

Also this is my Tkinter Function

def getInfo():
msg = entry.get()
print(type(msg))
print(msg)
search()

CodePudding user response:

def search(msg):  # add 'msg' as a parameter for the search function
    with open(r"loglar.txt", 'r') as fp:
        for l_no, line in enumerate(fp):
            lineNum = l_no   1
            # search string
            if msg in line:  # just use 'msg' here, not 'msg.get()'
                lineNumber = ('Line Number:', lineNum)
                lineWord = ('Line:', line)
                print(lineNumber)
                print(lineWord)


def getInfo():
    msg = entry.get()
    print(type(msg))
    print(msg)
    search(msg)  # search for msg


Now, when you call get_info() it will call search() for you using the contents of your entry as the msg parameter. You can, of course, also just call search(entry.get()) whenever you like.

  • Related