0

In my code, I could only get the output of one query. If I pass the next query in the entry box, the previous output in the textbox remains unchanged and I am not getting the output of my new query.

coding:-

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[^>]*>([^<]*)</\1>', r'\1-\2', s)
                        self.text.insert(tk.END, re.sub(r'<[^<>]*>','', m))  
app = SampleApp()
app.mainloop()

How can i get the next output after the passing the next query? Please help! Thanks in advance!

it adsd
  • 47
  • 1
  • 1
  • 5
  • first of all [don't try to parse XML with regexp](http://stackoverflow.com/a/1732454/128629) use a real XML parser what do you mean by "get the next output after the passing the next query" ? please give example of input/output desired – Xavier Combelle Mar 21 '15 at 10:50
  • @XavierCombelle if i pass " hello",it will produce the output of 'hello' related lines in that xml and produce in the text box..If i again write say "car" in entry box, the 'hello' persists, without updating the 'car' related lines over the text box. – it adsd Mar 21 '15 at 11:53
  • I think what happen is after entering "car" and press on the button, the "car" lines are at the end of the text box (after the "hello" lines). Do you want the "car" lines to replace the "hello" lines ? – Xavier Combelle Mar 21 '15 at 12:16
  • @XavierCombelle , yes exactly! – it adsd Mar 21 '15 at 13:22

1 Answers1

0

first of all don't try to parse XML with regexp use a real XML parser

I mean really stop parsing XML with a regexp because the regexp will probably not do what you want. (and when it will happen you will be really surprised).

To come back to your current problem you just have to delete all your Text widget content. One line at appropriate place should be enough (not tested)

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[^>]*>([^<]*)</\1>', r'\1-\2', s)
                        self.text.insert(tk.END, re.sub(r'<[^<>]*>','', m))  
app = SampleApp()
app.mainloop()
Community
  • 1
  • 1
Xavier Combelle
  • 10,968
  • 5
  • 28
  • 52