简体   繁体   English

tkinter 和 time.sleep

[英]tkinter and time.sleep

I am trying to delete text inside a text box after waiting 5 seconds, but instead the program wont run and does sleep over everything else.我试图在等待 5 秒后删除文本框中的文本,但程序不会运行并且会在其他所有内容上休眠。 Also is there a way for me to just make my textbox sleep so i can run other code while the text is frozen?还有一种方法可以让我的文本框休眠,这样我就可以在文本冻结时运行其他代码吗?

from time import time, sleep
from Tkinter import *

def empty_textbox():
    textbox.insert(END, 'This is a test')
    sleep(5)
    textbox.delete("1.0", END)

root = Tk()

frame = Frame(root, width=300, height=100)
textbox = Text(frame)

frame.pack_propagate(0)
frame.pack()
textbox.pack()

empty_textbox()

root.mainloop()

You really should be using something like the Tkinter after method rather than time.sleep(...) .你真的应该使用像 Tkinter after 方法而不是time.sleep(...)

There's an example of using the after method at this other stackoverflow question .在this other stackoverflow question中有一个使用after方法的示例。

Here's a modified version of your script that uses the after method:这是使用 after 方法的脚本的修改版本:

from time import time, sleep
from Tkinter import *

def empty_textbox():
    textbox.delete("1.0", END)

root = Tk()

frame = Frame(root, width=300, height=100)
textbox = Text(frame)

frame.pack_propagate(0)
frame.pack()
textbox.pack()

textbox.insert(END, 'This is a test')
textbox.after(5000, empty_textbox)

root.mainloop()

You can emulate time.sleep in tkinter. For this we still need to use the .after method to run our code alongside the mainloop , but we could add readability to our code with a sleep function. To add the desired behavior, tkinter provides another underestimated feature, wait_variable .您可以在time.sleep中模拟 time.sleep。为此,我们仍然需要使用.after方法与 mainloop 一起运行我们的代码,但我们可以通过sleep function 来增加代码的可读性。要添加所需的行为,tkinter 提供了另一个被低估的功能wait_variable wait_variable stops the codeblock till the variable is set and thus can be scheduled with after . wait_variable在设置变量之前停止代码块,因此可以使用after进行调度。

def tksleep(t):
    'emulating time.sleep(seconds)'
    ms = int(t*1000)
    root = tk._get_default_root('sleep')
    var = tk.IntVar(root)
    root.after(ms, var.set, 1)
    root.wait_variable(var)

Real world examples:现实世界的例子:

Limitation:局限性:

  • tkinter does not quit while tksleep is used. tkinter 在使用tksleep不会退出
    • Make sure there is no pending tksleep by exiting the application.通过退出应用程序确保没有挂起的 tksleep。
  • Using tksleep casually can lead to unintended behavior随便使用 tksleep 会导致意外行为

UPDATE更新

TheLizzard worked out something superior to the code above here . TheLizzard制定了比上面的代码更好的东西 Instead of tkwait command he uses the mainloop and this overcomes the bug of not quitting the process as described above, but still can lead to unintended output, depending on what you expect:他没有使用tkwait命令,而是使用mainloop ,这克服了如上所述不退出进程的错误,但仍然会导致意外的 output,具体取决于您的期望:

import tkinter as tk

def tksleep(self, time:float) -> None:
    """
    Emulating `time.sleep(seconds)`
    Created by TheLizzard, inspired by Thingamabobs
    """
    self.after(int(time*1000), self.quit)
    self.mainloop()
tk.Misc.tksleep = tksleep

# Example
root = tk.Tk()
root.tksleep(2)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM