Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

python - Delaying parts of text being inserted into Scroll Text Box (scrolledtext)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's a solution using the .after() method:

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

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

It uses a delayedInsert function which takes the text and delay in seconds and the global variable previousDelay to make the delays appear asynchronous (they are still happening at the same time but the delays are changed to make it appear like they are not). If the delays were not changed, each delay would start at the same time, not one after the other. The delayedInsert function waits for the delay specified plus the previous delay before inserting the text. This gives the same effect as time.sleep() but it works with Tkinter.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...