所以我想创建一个出现然后消失的文本,然后弹回另一个文本。 如果不使用文本创建多个标签,这是否可行?
这是我失败的尝试---->
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)
答案 0 :(得分:1)
是的,它可能而且相对简单。我简化了你的代码,但保持相对相同。
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()