Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 延迟部分文本插入滚动文本框(滚动文本)_Python_Tkinter_Text - Fatal编程技术网

Python 延迟部分文本插入滚动文本框(滚动文本)

Python 延迟部分文本插入滚动文本框(滚动文本),python,tkinter,text,Python,Tkinter,Text,我有一个按钮,当它被点击时,它会将文本插入到“滚动文本”框中 我想延迟部分文本插入文本框。如中所示,插入一行文本,延迟3秒,插入下一行文本,依此类推 我试图用“时间”来完成这项工作。但是,这只会延迟组合值插入的所有文本,然后立即插入所有文本。有没有办法让它按照我的愿望工作?有没有可能推迟,让每个字母一次插入一个 这是我尝试过的简化版本: import tkinter as tk from tkinter import * from tkinter import scrolledtext impo

我有一个按钮,当它被点击时,它会将文本插入到“滚动文本”框中

我想延迟部分文本插入文本框。如中所示,插入一行文本,延迟3秒,插入下一行文本,依此类推

我试图用“时间”来完成这项工作。但是,这只会延迟组合值插入的所有文本,然后立即插入所有文本。有没有办法让它按照我的愿望工作?有没有可能推迟,让每个字母一次插入一个

这是我尝试过的简化版本:

import tkinter as tk
from tkinter import *
from tkinter import scrolledtext
import time

# This is the GUI
trialGUI = Tk()
trialGUI.geometry('710x320')
trialGUI.title("Test GUI")

#This is the text that should be inserted when the button is pressed
def insertText():
    trialBox.insert(tk.INSERT, 'This line should be inserted first.\n')
    time.sleep(1)
    trialBox.insert(tk.INSERT, 'This line should be inserted after a 1 second delay.\n')
    time.sleep(3)
    trialBox.insert(tk.INSERT, 'This line should be inserted after a 3 second delay.\n')
    time.sleep(3)
    trialBox.insert(tk.INSERT, 'This line should be inserted after a 3 second delay.\n')

#This is the scrolling text box
trialBox = scrolledtext.ScrolledText(trialGUI, wrap = tk.WORD, width = 42, height = 10, font=(14))
trialBox.grid(row = 0, column = 0, columnspan = 4, pady = 3)

#This button runs the code to insert the text
trialButton = Button(trialGUI, text = "Run Code", command = insertText)
trialButton.grid(row = 1)

trialGUI.mainloop()

下面是一个使用
.after()
方法的解决方案:

def insertText():
    global previousDelay
    previousDelay = 0
    delayedInsert('This line should be inserted first.\n',0)
    delayedInsert('This line should be inserted after a 1 second delay.\n',1)
    delayedInsert('This line should be inserted after a 3 second delay.\n',3)
    delayedInsert('This line should be inserted after a 3 second delay.\n',3)

def delayedInsert(text, delay):
    global previousDelay
    trialGUI.after((delay + previousDelay) * 1000, lambda: trialBox.insert(tk.INSERT,text))
    previousDelay += delay

它使用一个
delayedInsert
函数,该函数以秒为单位获取文本和延迟,并使用全局变量
previousDelay
使延迟看起来是异步的(它们仍然在同一时间发生,但延迟会被更改,使其看起来不同步)。如果延迟没有改变,每个延迟将在同一时间开始,而不是一个接一个地开始。
delayedInsert
函数在插入文本之前等待指定的延迟加上上上一个延迟。这与
time.sleep()
具有相同的效果,但它与Tkinter一起工作。

after
方法进行一些研究,该方法允许您计划代码在将来运行。@BryanOakley我已经按照您的建议查看了该方法,但我不知道如何在代码中实现它。