我是Python的新手。该程序在Tk()窗口内创建一个标签。单词" hi"无限期地写入标签。我怎样才能删除旧的hi,同时仍然无限期地写新的hi?这甚至是我如何阻止内存泄漏?这是代码:
from tkinter import *
def box(a):
z=Label(root,text='%s'%(a))
z.place(width=50,height=20)
def start(root):
a="hi"
box(a)
root.after(100, start, root)
root = Tk()
start(root)
root.mainloop()
答案 0 :(得分:3)
如何只替换文本而不是每次都创建Label
对象。
from tkinter import *
def box(a):
z['text'] = a
def start(root):
box('hi')
root.after(100, start, root)
root = Tk()
z = Label(root, text='')
z.place(width=50, height=20)
start(root)
root.mainloop()
from tkinter import *
def start(root, z):
a = 'hi'
z['text'] = a
root.after(100, start, root, z)
root = Tk()
z = Label(root, text='')
z.place(width=50, height=20)
start(root, z)
root.mainloop()