it adsd
it adsd

Reputation: 47

displaying print output in a textbox

I have created a GUI IN tkinter,when i click on Get,it must produce the output results which is displayed using Print statement in my coding ,has to be displayeed in a textbox along with a scroll bar inside the same gui,once i click.

My code:-

import Tkinter as tk

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()

    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):
                        file1.write(row[x] + '\n')
                        s = row[x]
                        m = re.sub(r'<(\w+)\b[^>]*>([^<]*)</\1>', r'\1-\2', s)
                        print(re.sub(r'<[^<>]*>','', m))  #how to display it in textbox inside the same gui

app = SampleApp()
app.mainloop()

Please help me to acheive the output in a text box! Thanks in advance!

Upvotes: 1

Views: 17997

Answers (1)

CleverLikeAnOx
CleverLikeAnOx

Reputation: 1496

The widget you need is tk.Text. You could create the widget with the following lines in self.init:

self.text = tk.Text(self)
self.text.pack()

Then you can use the insert method to add text to it. The Text widget keeps track of a cursor and you have to say where to insert text into it. For example, to insert some text into the end of the box you could use:

self.text.insert(tk.END, "Some text here")

or in your program:

self.text.insert(tk.END, re.sub(r'<[^<>]*>','', m))

Instead of the Text widget you could also use ScrolledText from tkinter.scrolledtext. This gets you the scrollbar for free. Import it:

import tkinter.scrolledtext as tkst

and instantiate the widget in init:

self.text = tkst.ScrolledText(self)

Upvotes: 2

Related Questions