使Tkinter力为“a”;刷新“;mid命令中的文本小部件?

使Tkinter力为“a”;刷新“;mid命令中的文本小部件?,tkinter,Tkinter,我有一个Tkinter根目录,在其他项目中有一个文本小部件 当按下某个按钮时,它被配置为command=运行某个功能 在这个函数中有一个for循环,它对文本小部件进行10次快速刷新。我甚至在每次刷新之间设置了一个时间。sleep(0.1),这样不会太快 但是,当我运行它并按下按钮触发所有这一切时,我只看到所有发生的时间延迟,然后最终的结束结果最终显示在框中 在这个快速循环中,我如何强制它刷新文本小部件的内容 我将在下面粘贴完整的应用程序。您可以看到,您看到的第一个函数是快速刷新文本小部件的函数。

我有一个Tkinter根目录,在其他项目中有一个文本小部件

当按下某个按钮时,它被配置为command=运行某个功能

在这个函数中有一个for循环,它对文本小部件进行10次快速刷新。我甚至在每次刷新之间设置了一个时间。sleep(0.1),这样不会太快

但是,当我运行它并按下按钮触发所有这一切时,我只看到所有发生的时间延迟,然后最终的结束结果最终显示在框中

在这个快速循环中,我如何强制它刷新文本小部件的内容

我将在下面粘贴完整的应用程序。您可以看到,您看到的第一个函数是快速刷新文本小部件的函数。。。它叫dummycommand。谢谢--埃里克

从tkinter导入*
导入tkinter.messagebox
从随机导入*
导入时间
def dummycommand():
对于范围(10)内的r:
textoutput.delete(0.0,结束)
text输出.插入(结束,“|”*randint(1,10))
time.sleep(0.1)#我希望在这里可以强制刷新文本框
def hibox():
tkinter.messagebox.showinfo(“关于EricOwn”,“Tk-Try-Grid2”是在经过长期努力后于2018年设计和编写的”
“但还不如研究时期那样糟糕”
“我们不确定结果。”)
def辊()
textoutput.delete(0.0,结束)
textoutput.insert(END,str(randint(11000)),“标记中心”)
def quitcho():
root.quit()
root=Tk()
root.wm_标题(“GridGlob管理器v0.9”)
Text输出=文本(根,宽度=5,高度=1,bd=20,背景=“浅蓝色”,字体=(“Helvetica”,36))
textoutput.tag_configure('tag-center',justify='center'))
buttonroll=Button(root,text=“掷骰子”,command=Roll,activeforeground=“蓝色”,
activebackground=“红色”,background=“绿色”,foreground=“橙色”,padx=50)
buttonchars=Button(root,text=“显示字符”,command=dummycommand,activeforeground=“橙色”,
activebackground=“蓝色”,background=“黄色”,foreground=“浅蓝色”,padx=50)
label1=标签(根,文本=)
radiobutton\u widget1=radiobutton(根,text=“radiobutton 1”,值=1)
radiobutton\u widget2=radiobutton(根,text=“radiobutton 2”,值=2)
label2=标签(根,文本=)
buttonq=按钮(text=“Info”,command=hibox)
buttnr=按钮(text=“Quit”,command=quitto)
#==只是下拉菜单
菜单栏=菜单(根)
#=====
文件菜单=菜单(菜单栏)
filemenu.add_命令(label=“About EricOwn”,command=hibox)
filemenu.add_separator()
filemenu.add_命令(label=“Quit”,command=root.Quit,accelerator=“Ctrl+Q”)
menubar.add_级联(label=“File”,menu=filemenu)
#=====
mathmenu=菜单(菜单栏)
mathmenu.add_命令(label=“randomanness”,command=roll)
添加命令(label=“multiply”,command=dummycommand)
添加命令(label=“Dancing”,command=dummycommand)
添加命令(label=“Turtles”,command=dummycommand)
mathmenu.add_命令(label=“Trip to Ireland”,command=dummycommand)
mathmenu.add_命令(label=“三明治配好的酸面团”,command=dummycommand)
添加命令(label=“Western trot”,command=dummycommand)
mathmenu.add_分隔符()
mathmenu.add_命令(label=“English”,command=dummycommand)
mathmmenu.add_命令(label=“Social Studies”,command=dummycommand)
mathmenu.add_命令(label=“Geometry”,command=dummycommand)
添加命令(label=“Guess it!”,command=dummycommand)
菜单栏.add_级联(label=“Math”,menu=mathmenu)
#==现在将它们网格化
root.config(menu=menubar)
textoutput.grid(行=10,列=0,列span=2)
按钮滚动网格(行=20,列=0)
buttonchars.grid(行=20,列=1)
标签1.网格(行=30)
radiobutton_widget1.grid(行=40,列=0,列span=2)
radiobutton_widget2.grid(行=50,列=0,列span=2)
标签2.网格(行=60)
按钮Q.网格(行=70,列=0)
按钮网格(行=70,列=1)
root.grid\u columnconfigure(0,minsize=200)
root.grid\u columnconfigure(1,minsize=200)

root.mainloop()。使用
根目录。改为在
之后:

def dummycommand():
    textoutput.delete(0.0, END)
    textoutput.insert(END, "|" * randint(1, 10))
    root.after(100, dummycommand)
为了限制重复次数,可以使用具有默认值的关键字参数:

def dummycommand(n_times=0, default=10):
    n = n_times + 1
    if n <= default:
        textoutput.delete(0.0, END)
        textoutput.insert(END, "|" * randint(1, 10))
        root.after(100, dummycommand, n)
def dummycommand(n_次=0,默认值=10):
n=n_次+1

如果n
time.sleep
正在阻止所有进程并冻结GUI。使用
根目录。改为在
之后:

def dummycommand():
    textoutput.delete(0.0, END)
    textoutput.insert(END, "|" * randint(1, 10))
    root.after(100, dummycommand)
为了限制重复次数,可以使用具有默认值的关键字参数:

def dummycommand(n_times=0, default=10):
    n = n_times + 1
    if n <= default:
        textoutput.delete(0.0, END)
        textoutput.insert(END, "|" * randint(1, 10))
        root.after(100, dummycommand, n)
def dummycommand(n_次=0,默认值=10):
n=n_次+1

这是一个很好的输入,但似乎只起到了一半的作用。我将其更改为0.1秒,并插入以下行:root.after(100,dummycommand)。。。然而,发生的是:也许在前5次迭代中,它很好地快速更新了文本框。但是在5次之后它就冻结了,整个tkinter/python应用程序在我的mac上冻结,显示彩虹球,我不得不强制退出该应用程序。你有什么想法吗?:)谢谢--您应该用我的答案中的函数替换您的函数
dummycommand
;只要你还有通话时间,插入一条线路就不会起作用。睡眠对不起,我应该解释得更清楚些。我确实用你的行替换了sleep命令。因为我知道睡眠是问题的根源。。。ug,那么,关于为什么会发生冻结有什么想法吗?不,我没有提供“一行”,而是替换了您的整个功能。如果将
root.after
放在循环中,它将不起作用!噢!!很抱歉我需要更仔细地观察这些东西!让我试一试,非常感谢——嗯——这是一个很好的输入,但它似乎只起到了一半的作用。我将其更改为0.1秒,并插入以下行:root.after(100,dummycommand)。。。然而,发生的是:也许在前5次迭代中,它很好地更新了