1

This program prints whole of the string after a delay of 0.5.I want to print each character after a delay of 0.5 .How to do it.Please help me.This program prints whole of the word after a delay

import tkinter as tk
import time
class tab:
    i=0
    def __init__(self,master):
        self.input=tk.Entry(master,text="Input")
        self.input.grid(column=1,row=1)
        self.button=tk.Button(master,text="Click me!",command=self.printt)
        self.button.grid(column=1,row=2)
        self.label=tk.Label(master,text="")
        self.label.grid(column=1,row=3)
    def printt(self):
        try:
            t=str(self.input.get())
            tab.i=0
            while(tab.i< len(t)):
                  self.label['text']=self.label['text'] + t[tab.i]
                  time.sleep(0.5)
                  tab.i = tab.i + 1

        except ValueError:
            self.label['text']="Error"
            return



root=tk.Tk()
tab(root)
root.geometry('300x400')
root.mainloop()
gokul
  • 109
  • 10
  • 0.5 what? as in 30 seconds? 50 seconds? The sleep function takes argument in seconds. – Trollsors Oct 15 '19 at 07:42
  • Possible duplicate of [Printing a string with a little delay between the chars](https://stackoverflow.com/questions/4627033/printing-a-string-with-a-little-delay-between-the-chars) – Gray_Rhino Oct 15 '19 at 07:43
  • On clicking the button whole of the word gets displayed after a certain delay.What i want is that the characters get printed on the window one by one after the delay – gokul Oct 15 '19 at 07:55
  • Possible duplicate of [tkinter: how to use after method](https://stackoverflow.com/questions/25753632/tkinter-how-to-use-after-method) – Henry Yik Oct 15 '19 at 08:21

1 Answers1

1

I got this code from python mailing list.Friends thanks for all your support and help

import tkinter as tk

class App(tk.Tk):
    def __init__(self,*args, **kwargs):
         tk.Tk.__init__(self, *args, **kwargs)
         self.label = tk.Label(self, text="", width=20, anchor="w")
         self.label.pack(side="top",fill="both",expand=True)
         self.print_label_slowly("Hello, world!")

    def print_label_slowly(self, message):
         '''Print a label one character at a time using the event loop'''
         t = self.label.cget("text")
         t += message[0]
         self.label.config(text=t)
         if len(message) > 1:
             self.after(500, self.print_label_slowly, message[1:])

app = App()
app.mainloop()
gokul
  • 109
  • 10