Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python canvas.coord()累积内存_Python_User Interface_Memory Leaks_Tkinter - Fatal编程技术网

python canvas.coord()累积内存

python canvas.coord()累积内存,python,user-interface,memory-leaks,tkinter,Python,User Interface,Memory Leaks,Tkinter,我用tkinter制作了一个简单的模拟时钟来研究python GUI。代码如下: from tkinter import * from datetime import datetime import threading from math import cos, sin, pi root = Tk() root.geometry('400x400') pause = True def sec_to_coord(second): x = cos((second - 15) * pi /

我用tkinter制作了一个简单的模拟时钟来研究python GUI。代码如下:

from tkinter import *
from datetime import datetime
import threading
from math import cos, sin, pi

root = Tk()
root.geometry('400x400')
pause = True

def sec_to_coord(second):
    x = cos((second - 15) * pi / 30)
    y = sin((-second - 15) * pi / 30)
    return x,y

def update():
    global pause
    if pause: return
    threading.Timer(1, update).start()
    now = datetime.now()
    x,y = sec_to_coord(now.second)
    canv.coords(line, 200, 200, x * 100 + 200, y * 100 + 200) # drains RAM
    x,y = sec_to_coord(now.minute + now.second / 60)
    canv.coords(minhand, 200, 200, x * 90 + 200, y * 90 + 200) # drains RAM
    x,y = sec_to_coord(now.hour * 5 + now.minute / 12)
    canv.coords(hourhand, 200, 200, x * 50 + 200, y * 50 + 200) # drains RAM
    print(':'.join([str(now.hour), str(now.minute), str(now.second)]))

def start():
    global pause
    if not pause: return
    pause = False
    print('pause =', pause)
    update()

def halt():
    global pause
    if not pause: 
        pause = True
        print('pause =', pause)

def on_closing():
    halt()
    print('BUY-BUY!')
    root.destroy()

canv = Canvas(root, width=400, height=400)
canv.pack()
line = canv.create_line(200, 200, 200, 100)
minhand = canv.create_line(200, 200, 200, 110, width = 3)
hourhand = canv.create_line(200, 200, 200, 150, width= 7)
ticks = []
for i in range(60):
    x,y = sec_to_coord(i)
    if i % 5 == 0:
        ticks.append(canv.create_line(x*90+200, y*90+200, \
            x*100+200, y*100+200, width=3))
    else:
        ticks.append(canv.create_line(x*95+200, y*95+200, \
            x*100+200, y*100+200, width=3))

root.protocol('WM_DELETE_WINDOW', on_closing)
start()
root.mainloop()
问题是:时钟在滴答作响时会累积RAM。通过对部分代码进行注释,我发现
def update():
中的
canv.coord()
函数负责。
请问,谁能告诉我代码有什么问题吗?

谢谢你,特里!!!这就解决了问题!我不得不按照您的建议只更改一行
root.after(1000,update)
而不是
threading.Timer(1,update).start()
和RAM累积消失了

tkinter不是线程安全的。tkinter中有一些不需要线程的简单动画制作技术。@BryanOakley:tkinter是否(相对于预期的)线程安全似乎取决于术语的定义和Python版本。请参阅和Martin的评论以及。在这里,我报告2.7中失败的线程tkinter代码在3.5中起作用。使用
root.after(1000,update)
代替计时器调用。它使用tk循环执行大致相同的操作。使用
root.after
查看动画中的其他答案。