延迟插入滚动文本框的文本部分(滚动文本) [英] Delaying parts of text being inserted into Scroll Text Box (scrolledtext)

查看:29
本文介绍了延迟插入滚动文本框的文本部分(滚动文本)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个按钮,可以在单击时将文本插入到滚动文本"框中.

I have a button which inserts text into a 'scrolltext' box when it is clicked.

我想延迟插入文本框的部分文本.例如,插入一行文本,延迟 3 秒,插入下一行文本,依此类推...

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.\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() 方法的解决方案:

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

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.

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.

这篇关于延迟插入滚动文本框的文本部分(滚动文本)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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