【问题标题】:(Python 3.7) How can i print messages characters with a delay between them using tkinter?(Python 3.7)如何使用 tkinter 打印消息字符之间有延迟?
【发布时间】:2020-06-16 02:28:16
【问题描述】:

首先我是 python 和编码的新手

我想用 tkinter 做一些非常简单的事情,当你点击一个按钮时,它会向你显示一个文本,就像在旧游戏中一样,一个字母一个字母地显示,每个字符之间会有一点延迟

我找不到让字符之间的延迟的方法,我尝试了 time.sleep 循环,但文本显示在循环的末尾

我见过 after 功能,但我不知道如何使用它,也不了解它是如何工作的

我应该使用 sleep 还是 after ?我应该如何使用它们来使其工作?

顺便说一句,如果您对代码有任何提示或建议,请告诉我

    #MainFrame
root.title("Project")
root.geometry('400x400')
root.configure(bg="plum1")
    #Frame
BlackBorder=Frame(root,width=400,height=300,bg='Black')
BlackBorder.place(x=0,y=80)
TxtFrame=Frame(BlackBorder,width=370,height=270,bg='lavender')
TxtFrame.place(x=15,y=15)
    #Display
Cunter=Text(root,width=24,height=1,bg='lavender',font='Fixedsys')
Cunter.place(x=100,y=22)
Cunter.insert(END, str(len(LoList))+" Résultats Différents")


#defTxt
def LoMsg(self):
    self=Text(TxtFrame,wrap='word',borderwidth=0,width=35,height=10,bg='lavender',font='Fixedsys')
    self.place(x=50,y=100)
    LoTxt=str(LovList[randrange(len(LovList))])
    LoNum=0
    while LoNum!=len(LoTxt):
        self.insert(END,LoTxt[LoNum])
        sleep(0.1)
        LoNum+=1

    #Button
buttonMain=Button(root,width=9,height=3,bg='thistle2',text="Try me",font='Fixedsys')
buttonMain.place(x=5,y=5)
#ButtonEvent
buttonMain.bind('<1>', LoMsg)

【问题讨论】:

    标签: python loops tkinter sleep


    【解决方案1】:

    欢迎使用 Python 和编码!在回答您的问题之前,我想解决一些问题:

    1. 如果您在提问时可以提供minimal, reproducible example,这将很有帮助。我真的看不出你的代码中的问题是什么,因为发生了很多事情,我无法在我的计算机上运行它,因为缺少部分。例如,LoList 未定义且没有 import 语句。)

    2. PEP8 提供了大量关于如何设置代码样式的建议 - 太多了,无法一次阅读所有内容。但是我想提请您注意Function and Variable names,它表明变量和函数都使用小写字母,单词之间用下划线分隔。通常Class Names 以大写字母开头。所以你的很多变量和函数在我看来就像类。

    为了回答您的问题,我认为使用time.sleep() 会起作用。下面是一个在控制台中运行的简单示例,可以帮助您了解如何使用它:

    import time
    text = "I've been awaiting your arrival..."
    for char in text:
        # end='' will prevent each letter from getting its own line on the console
        # flush=True will make sure that the character is printed right away in the console
        print(char, end='', flush=True) 
        time.sleep(0.1)
    

    【讨论】:

    • 你不应该在 GUI 的主线程中调用 time.sleep。它阻止了 GUI 能够处理事件,包括请求调整窗口大小、重绘窗口等事件。
    • OP 已经在使用time.sleep,那么你的解决方案和 OP 有什么区别呢? OP 的问题是文本没有按预期逐个字符显示。
    【解决方案2】:

    下面是一个例子,强调使用after(ms, callback) 方法来获得你想要的结果(相应地调整 after 方法中的ms):

    import tkinter as Tk
    
    def insert():
        global LoNum
        text.insert(Tk.END, word[LoNum])
        LoNum += 1
        if LoNum != len(word):
            root.after(300, insert)
        else:
            return
    
    root = Tk.Tk()
    root.geometry('600x200')
    
    LoNum = 0
    word = [x for x in 'abcdefg'] # get the word to a list format
    text = Tk.Text(root, wrap='word', borderwidth=0, width=35, height=10, bg='lavender')
    text.pack()
    
    Tk.Button(root, text='Try Me', width=9, height=3, bg='thistle2', command=insert).pack()
    
    root.mainloop()
    

    【讨论】:

    • 首先,可以在insert()中删除else块。其次,如果Try Me按钮被第二次点击,会出现LoNum没有重置的错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多