I have a button which inserts text into a 'scrolltext' box when it is clicked.
I want to delay parts of the text being inserted into the text box. As in, one line of text is insert, there a 3 second delay, the next line of text is inserted and so on...
The I attempted using 'time' to make this work. However, this just delays all the text being inserted by the combined value and then all the text is inserted at once. Is there a way to make it work how I wish? And is it possible to delay it, so that each letter is inserted one at a time?
This is a much simplified version of what I've tried:
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.
')
time.sleep(1)
trialBox.insert(tk.INSERT, 'This line should be inserted after a 1 second delay.
')
time.sleep(3)
trialBox.insert(tk.INSERT, 'This line should be inserted after a 3 second delay.
')
time.sleep(3)
trialBox.insert(tk.INSERT, 'This line should be inserted after a 3 second delay.
')
#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()
question from:
https://stackoverflow.com/questions/65935489/delaying-parts-of-text-being-inserted-into-scroll-text-box-scrolledtext 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…