为什么Tkinter函数仅在函数完成后才在文本小部件中插入文本? [英] 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 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()
推荐答案
仅在进入事件循环时才刷新文本窗口小部件(和所有窗口小部件).当您的代码处于循环中时,不会处理任何绘画事件.最简单的解决方案是调用 update_idletasks
来更新屏幕-刷新屏幕被视为空闲"任务.
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.
有关更多信息,请参阅问题的答案如何在Tkinter中更新窗口小部件?
For a little more information, see the answers to the question How do widgets update in Tkinter?
这篇关于为什么Tkinter函数仅在函数完成后才在文本小部件中插入文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!