I have a code where I am trying to print list of lists in tkinter label but it doesn't work the way I want. When I run the script below it will search the entered text in list.txt and print it in tkinter label. As more text is entered, the previous searched text should clear and new text should appear based on search results in list.txt file. But the previous text doesn't get cleared.Here's a partial code:
import tkinter as tk
import re
from tkinter import Label
class App(tk.Tk):
def __init__(self):
super().__init__()
self.var = tk.StringVar()
self.var.trace("w", self.show_message)
self.entry = tk.Entry(self, textvariable=self.var)
self.label = tk.Label(self)
self.entry.pack()
self.label.pack()
def show_message(self, *args):
value = self.var.get()
scen = []
text = "{}".format(value) if value else scen.clear()
if text:
words = re.split('[ ]', text.lower())
with open('list.txt','r') as mfl:
scen.clear()
for line in mfl:
if all(word in line.lower() for word in words):
txt = re.split("[._ -]",line)
scen.append(txt[:-1])
for i in range(len(scen)):
exec('Label%d=Label(app,text="%s")\nLabel%d.pack()'%(i,scen[i],i))
if __name__ == "__main__":
app = App()
app.mainloop()