The_Town_boolean
The_Town_boolean

Reputation: 67

Tkinter make text that changes over time

So i wanted to create a text that appears then disappears then pops back as a different text. Is this possible without creating multiple labels with text ?

here was my failed attempt ---->

from tkinter import*
class App():
   def __init__(self,master):
       self.master=master

       dialog=['This is my text thats going to dissapear','farts are fun']

       for i in range(len(dialog)):
           self.s_var=StringVar()

           self.label = Label(self.master,textvariable=self.s_var,font='times')
           self.label.place(x=0, y=0)
           self.s_var.set(dialog[i])

           self.label.after(10000, self.clear_label)    # 1000ms
        self.master.mainloop()
    def clear_label(self):
        self.label.place_forget()
root=Tk()
app=App(root)

Upvotes: 2

Views: 161

Answers (1)

Pythonista
Pythonista

Reputation: 11625

Yes, it's possible and relatively simple. I simplified your code some but kept it relatively the same.

import tkinter as tk

class App:

    def __init__(self, master):

        self.dialog_options = ['This is my text thats going to dissapear', 'farts are fun']
        self.label = tk.Label(master, text=self.dialog_options[0])
        self.label.pack()
        self.label.after(10000, self.change_label_text)    # 1000ms

    def change_label_text(self):

        self.label['text'] = self.dialog_options[1]

if __name__ == '__main__':

    root= tk.Tk()
    app = App(root)
    root.mainloop()

Upvotes: 1

Related Questions