G V Sandeep
G V Sandeep

Reputation: 233

How to reinitialize text widget in Tkinter?

I am trying to return the search result of the user in the GUI itself. This is the following code which I am using

def printResult(searchResult):
global resultsFrame
text = Text(resultsFrame)

for i in searchResult:
    text.insert(END,i+'\n')
text.pack(side=TOP) 

where searchResult is a list which has all the statements to be printed on the screen and this is how I declared resultsFrame in the code (before root.mainloop()) I am using global so that it makes changes to the existing frame but it rather adds another frame below it. How to make changes in the already existing text widget?

resultsFrame = Frame(root)
resultsFrame.pack(side=TOP)

For reference here is an image of the GUI:

Creation of new Text widget

EDIT : Issue Solved

I declared the frame outside the function

resultsFrame = Frame(root)
resultsFrame.pack(side=TOP)
text = Text(resultsFrame)

def printResult(searchResult):
   global resultsFrame
   global text
   text.delete("1.0",END)
   for i in searchResult:
       text.insert(END,str(i)+'\n')
   text.pack(side=TOP) 

Upvotes: 0

Views: 250

Answers (1)

Ravi Shankar Bharti
Ravi Shankar Bharti

Reputation: 9268

Try this:

global resultsFrame
text = Text(resultsFrame)

def printResult(searchResult):
    for i in searchResult:
        text.insert(END,i+'\n')
...
text.pack(side=TOP) 

This should solve your issue.

Upvotes: 2

Related Questions