tkinter का उपयोग कर पीआईएल छवियों के अनुक्रम को एनिमेट करने की कोशिश कर रहा है। मेरे फ्रेम अवधि (एमएस) का ग्राफ इस तरह दिखता है: साउथोथ टिंकर मेनलोप फ्रेम अवधि?
किसी को भी यह पता है कि इस स्पाकी आर्टूओथ पैटर्न का कारण क्या हो सकता है?
from PIL import Image, ImageTk
import Tkinter
import time
import sys
def generate_frames(n):
"""
keep n under 101 * 101
"""
out = []
last_pil = None
for i in range(n):
if last_pil:
pil_image = last_pil.copy()
else:
pil_image = Image.new('L', (101, 101), 255)
x = i/101
y = i % 101
pil_image.load()[x, y] = 0
out.append(ImageTk.PhotoImage(pil_image))
last_pil = pil_image
return out
def draw():
FRAME_COUNT =5000
master = Tkinter.Tk()
w = Tkinter.Canvas(master, width=302, height=302)
w.create_rectangle(49, 49, 252, 252)
w.pack()
frames = generate_frames(FRAME_COUNT)
def draw_frame(f, canvas_image):
print repr(time.time())
frame = frames[f]
if canvas_image is None:
canvas_image = w.create_image((151, 151), image=frame, anchor='center')
else:
w.itemconfigure(canvas_image, image=frame)
w.current_frame = frame # save a reference
next_frame = f + 1
if next_frame < FRAME_COUNT:
master.after(1, draw_frame, next_frame, canvas_image)
else:
sys.exit(0)
master.after(10, draw_frame, 0, None)
master.mainloop()
draw()
भूखंड देखने के लिए, पाइप उत्पादन
import sys
last = None
for line in sys.stdin:
value = float(line.strip()) * 1000
if last is None:
pass
else:
print (value - last)
last = value
तो
के माध्यम से के माध्यम से
from matplotlib import pyplot
import sys
X = []
Y = []
for index, line in enumerate(sys.stdin):
line = line.strip()
X.append(index)
Y.append(float(line))
pyplot.plot(X, Y, '-')
pyplot.show()
बनाना यह मल्टी-थ्रेडेड मदद नहीं करता है:
यहाँ पुन: पेश करने के लिए एक स्क्रिप्ट है :
class AnimationThread(threading.Thread):
FRAME_COUNT = 5000
def __init__(self, canvas):
threading.Thread.__init__(self)
self.canvas = canvas
self.frames = generate_frames(self.FRAME_COUNT)
def run(self):
w = self.canvas
frames = self.frames
canvas_image = None
for i in range(self.FRAME_COUNT):
print repr(time.time())
frame = frames[i]
if canvas_image is None:
canvas_image = w.create_image((151, 151), image=frame, anchor='center')
else:
w.itemconfigure(canvas_image, image=frame)
w.current_frame = frame
time.sleep(1 * .001)
def draw_threaded():
FRAME_COUNT = 5000
master = Tkinter.Tk()
w = Tkinter.Canvas(master, width=302, height=302)
w.create_rectangle(49, 49, 252, 252)
w.pack()
animation_thread = AnimationThread(w)
animation_thread.start()
master.mainloop()
animation_thread.join()
draw_threaded()
मैं एनीमेशन को एक अलग थ्रेड में चलाने का प्रयास करूंगा , tkinter mainloop में नहीं और देखें कि क्या होता है। –
अच्छा विचार - धन्यवाद। हालांकि इसमें कोई फर्क नहीं पड़ता (संपादित पोस्ट देखें) – sobel
क्या आपने इसे 'पायथन-एम cprofile' के साथ प्रोफाइल करने का प्रयास किया है? –