【问题标题】:Tkinter make text that changes over timeTkinter 制作随时间变化的文本
【发布时间】:2017-11-20 07:18:46
【问题描述】:

所以我想创建一个出现然后消失然后作为不同文本弹回的文本。 如果不使用文本创建多个标签,这可能吗?

这是我失败的尝试---->

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)

【问题讨论】:

    标签: python class variables tkinter label


    【解决方案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()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-13
      • 2013-07-05
      • 2020-01-09
      • 1970-01-01
      相关资源
      最近更新 更多