使 Tkinter 强制“刷新"中间命令中的文本小部件? [英] Make Tkinter force a "refresh" a text widget in mid-command?

查看:24
本文介绍了使 Tkinter 强制“刷新"中间命令中的文本小部件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 Tkinter 根目录,其中包含一个文本小部件以及其他项目.

I have a Tkinter root that has a text widget among other items.

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

When a certain button is pressed, it is configured with command= to run a certain function, of course.

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

In that function is a for-loop that does 10 rapid refreshes of the text widget. I even put a time.sleep(0.1) between each refresh so it would not be too fast.

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

But, when I run it and press that button to trigger all this, I only see the lag in time while that all happens, and then the final ending result is finally shown in the box.

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

How can I, inside that rapid loop, force it to refresh the contents of that text widget?

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

I will paste the full app below. You can see that the first function you see is the one that does the rapid refreshing of the text widget... it's called dummycommand. THANKS! --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()

推荐答案

time.sleep 正在阻止所有进程并冻结您的 GUI.使用 root.after 代替:

time.sleep is blocking all processes and freezes your GUI. Use root.after instead:

def dummycommand():
    textoutput.delete(0.0, END)
    textoutput.insert(END, "|" * randint(1, 10))
    root.after(100, dummycommand)

为了限制重复次数,可以使用关键字参数,默认值:

In order to limit the number of repeats, you can use key word arguments, with a default value:

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)

这篇关于使 Tkinter 强制“刷新"中间命令中的文本小部件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆