让Tkinter在命令中强制“刷新”文本小部件?

时间:2018-06-17 03:48:27

标签: tkinter

我有一个Tkinter根,其中包含一个文本小部件。

当按下某个按钮时,它配置有command =来运行某个功能,当然。

在该函数中是一个for循环,它可以对文本小部件进行10次快速刷新。我甚至在每次刷新之间放了一个time.sleep(0.1),所以它不会太快。

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

如何在快速循环中强制它刷新该文本小部件的内容?

我将粘贴下面的完整应用。您可以看到,您看到的第一个函数是快速刷新文本小部件的函数...它被称为dummycommand。谢谢! --eric

from tkinter import *
import tkinter.messagebox   
from random import *
import time

def dummycommand():
    for r in range (10):
        textoutput.delete(0.0, END)
        textoutput.insert(END,"|"*randint(1,10))
        time.sleep(0.1)   # THIS IS WHERE I WISH I COULD HAVE IT FORCE A REFRESH OF THE TEXT BOX

def hibox():
    tkinter.messagebox.showinfo("About EricOwn", "Tk-Try-Grid2 was designed and written in 2018 after a long"
                                                 " but not quite as bad research period where"
                                                 " we are not certain of the outcome.")
def roll():
    textoutput.delete(0.0,END)
    textoutput.insert(END,str(randint(1,1000)), 'tag-center')

def quitto():
    root.quit()

root=Tk()
root.wm_title("GridGlob Manager v0.9")

textoutput = Text(root,width=5,height=1,bd=20, background="light blue",font=("Helvetica", 36))
textoutput.tag_configure('tag-center', justify='center')

buttonroll = Button(root,text="Roll the dice", command=roll, activeforeground="blue",
                 activebackground="red",background="green",foreground="orange",padx=50)

buttonchars = Button(root,text="Show the chars", command=dummycommand, activeforeground="orange",
                 activebackground="blue",background="yellow",foreground="light blue",padx=50)

label1 = Label(root,text=" ")

radiobutton_widget1 = Radiobutton(root,text="Radiobutton 1", value=1)
radiobutton_widget2 = Radiobutton(root,text="Radiobutton 2", value=2)

label2 = Label(root,text=" ")


buttonq=Button(text="Info", command=hibox)
buttonr=Button(text="Quit", command=quitto)



# === Just the pulldown menus

menubar = Menu(root)
#=====
filemenu = Menu(menubar)
filemenu.add_command(label="About EricOwn", command=hibox)
filemenu.add_separator()
filemenu.add_command(label="Quit",command=root.quit, accelerator="Ctrl+Q")
menubar.add_cascade(label="File", menu=filemenu)
#=====
mathmenu = Menu(menubar)
mathmenu.add_command(label="Randomness", command=roll)
mathmenu.add_command(label="Multiplication",command=dummycommand)
mathmenu.add_command(label="Dancing",command=dummycommand)
mathmenu.add_command(label="Turtles",command=dummycommand)
mathmenu.add_command(label="Trip to Ireland",command=dummycommand)
mathmenu.add_command(label="Sandwich with good sourdough",command=dummycommand)
mathmenu.add_command(label="Western trot",command=dummycommand)
mathmenu.add_separator()
mathmenu.add_command(label="English",command=dummycommand)
mathmenu.add_command(label="Social Studies",command=dummycommand)
mathmenu.add_command(label="Geometry",command=dummycommand)
mathmenu.add_command(label="Guess it!",command=dummycommand)
menubar.add_cascade(label="Math", menu=mathmenu)





# === Now grid them

root.config(menu=menubar)

textoutput.grid(row=10, column=0, columnspan=2)
buttonroll.grid(row=20, column=0)
buttonchars.grid(row=20,column=1)

label1.grid(row=30)

radiobutton_widget1.grid(row=40, column=0, columnspan=2)
radiobutton_widget2.grid(row=50, column=0, columnspan=2)

label2.grid(row=60)

buttonq.grid(row=70, column=0)
buttonr.grid(row=70, column=1)

root.grid_columnconfigure(0, minsize=200)
root.grid_columnconfigure(1, minsize=200)

root.mainloop()

1 个答案:

答案 0 :(得分:2)

time.sleep阻止所有进程并冻结您的GUI。请改用root.after

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)