Why Tkinter function inserts text at text widget only after the function is finished?
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 process开发者_如何转开发ing 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()
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?
精彩评论