0

I have a question to make.If i have a function inserting an item from a list in a text widget and then doing something with that item,it first finishes processing all the items and then does the insert on the text widget.

Here is a simple code demonstrating what I say:

from Tkinter import*
import Tkinter as tk

list = range(1,1000)

def highlow():
    for numbers in list:
       text1.insert(END,'Number : '+str(numbers)+'\n')
    if numbers>500:
       print 'High'
    else:
       print 'Low'

app=Tk()
app.title("Window Title")
app.geometry('400x400+200+200')
app.resizable(0,0)

button=Button(app,text="Run",font=("Times", 12, "bold"), width=20 ,borderwidth=5, foreground = 'white',background = 'blue',command=highlow)
button.pack()

text1 = Text(app,height = 60,font=("Times", 12))
text1.insert(END,"")
text1.pack(padx=15,pady=1)


app.mainloop()
IordanouGiannis
  • 4,149
  • 16
  • 65
  • 99
  • 1
    What is the question? Please edit your post to add a clear question. Also, please make the title of your question more specific. – agf Aug 16 '11 at 09:05
  • There still is no question in your post, just a sentence at the top and some code. I'm going to vote to close as "Not a real question" if you don't add one. – agf Aug 16 '11 at 09:28
  • The question is that the text is inserted in the text widget for every item of a list only when the function has ended processing all the items of a list. – IordanouGiannis Aug 16 '11 at 09:44
  • That still isn't a question. A question would be "Why does the text get inserted after the function is finished?" I'll answer it. – agf Aug 16 '11 at 09:52

1 Answers1

1

The text widget (and all widgets) are only refreshed when the event loop is entered. While your code is in a loop, no paint events are processed. The simplest solution is to call update_idletasks to update the screen -- refreshing the screen is considered an "idle" task.

For a little more information, see the answers to the question How do widgets update in Tkinter?

Community
  • 1
  • 1
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685