即使在通过下一个输入后,先前的输出仍然存在

previous output remains even after passing next input

在我的代码中,我只能得到一个查询的输出。如果我在输入框中传递下一个查询,文本框中之前的输出将保持不变,并且我不会获得新查询的输出。

编码:-

import Tkinter as tk
import re
class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.L1 = tk.Label(self, text="Enter the query")
        self.L1.pack()        
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.pack()
        self.entry.pack()
        self.text = tk.Text(self)
        self.text.pack()
    def on_button(self):
        s1=(self.entry.get())

        with open("myxml.txt","rb")as f:
             row = f.readlines()
             for i, r in enumerate(row):
                if s1 in r:           
                    for x in range(i-3,i+4):
                        s = row[x]
                        m = re.sub(r'<(\w+)\b[^>]*>([^<]*)</>', r'-', s)
                        self.text.insert(tk.END, re.sub(r'<[^<>]*>','', m))  
app = SampleApp()
app.mainloop()

如何在通过下一个查询后获得下一个输出? 请帮忙!提前致谢!

首先don't try to parse XML with regexp使用真正的XML解析器

我的意思是真的停止用正则表达式XML解析XML因为正则表达式可能不会做你想要的。 (当它发生时你会非常惊讶)。

回到您当前的问题,您只需删除所有文本小部件内容。适当的地方一行就够了(未测试)

import Tkinter as tk
import re
class SampleApp(tk.Tk):
    def __init__(self):
        tk.Tk.__init__(self)
        self.L1 = tk.Label(self, text="Enter the query")
        self.L1.pack()        
        self.entry = tk.Entry(self)
        self.button = tk.Button(self, text="Get", command=self.on_button)
        self.button.pack()
        self.entry.pack()
        self.text = tk.Text(self)
        self.text.pack()
    def on_button(self):
        s1=(self.entry.get())

        #here is the line added
        self.text.delete(1.0, tk.END) #delete the content of self.text

        with open("myxml.txt","rb")as f:
             row = f.readlines()
             for i, r in enumerate(row):
                if s1 in r:           
                    for x in range(i-3,i+4):
                        s = row[x]
                        m = re.sub(r'<(\w+)\b[^>]*>([^<]*)</>', r'-', s)
                        self.text.insert(tk.END, re.sub(r'<[^<>]*>','', m))  
app = SampleApp()
app.mainloop()