MCPcopy Index your code
hub / github.com/PySimpleGUI/PySimpleGUI / update_animation

Method update_animation

PySimpleGUI/PySimpleGUI.py:5923–5979  ·  view source on GitHub ↗

Show an Animated GIF. Call the function as often as you like. The function will determine when to show the next frame and will automatically advance to the next frame at the right time. NOTE - does NOT perform a sleep call to delay :param source: Filename or Bas

(self, source, time_between_frames=0)

Source from the content-addressed store, hash-verified

5921 self._visible = visible
5922
5923 def update_animation(self, source, time_between_frames=0):
5924 """
5925 Show an Animated GIF. Call the function as often as you like. The function will determine when to show the next frame and will automatically advance to the next frame at the right time.
5926 NOTE - does NOT perform a sleep call to delay
5927 :param source: Filename or Base64 encoded string containing Animated GIF
5928 :type source: str | bytes | None
5929 :param time_between_frames: Number of milliseconds to wait between showing frames
5930 :type time_between_frames: (int)
5931 :return: True if worked OK, False if out of frames, None is window was closed
5932 :rtype: True | False | None
5933 """
5934
5935 if self.Source != source:
5936 self.AnimatedFrames = None
5937 self.Source = source
5938
5939 done = False
5940
5941 if self.AnimatedFrames is None:
5942 self.TotalAnimatedFrames = 0
5943 self.AnimatedFrames = []
5944 # Load up to 1000 frames of animation. stops when a bad frame is returns by tkinter
5945 for i in range(1000):
5946 if type(source) is not bytes:
5947 try:
5948 self.AnimatedFrames.append(tk.PhotoImage(file=source, format='gif -index %i' % (i)))
5949 except Exception as e:
5950 break
5951 else:
5952 try:
5953 self.AnimatedFrames.append(tk.PhotoImage(data=source, format='gif -index %i' % (i)))
5954 except Exception as e:
5955 break
5956 self.TotalAnimatedFrames = len(self.AnimatedFrames)
5957 self.LastFrameTime = time.time()
5958 self.CurrentFrameNumber = -1 # start at -1 because it is incremented before every frame is shown
5959 # show the frame
5960
5961 now = time.time()
5962
5963 if time_between_frames:
5964 if self.CurrentFrameNumber+1 == self.TotalAnimatedFrames:
5965 done = True
5966 if (now - self.LastFrameTime) * 1000 > time_between_frames:
5967 self.LastFrameTime = now
5968 self.CurrentFrameNumber = (self.CurrentFrameNumber + 1) % self.TotalAnimatedFrames
5969 else: # don't reshow the frame again if not time for new frame
5970 return True
5971 else:
5972 self.CurrentFrameNumber = (self.CurrentFrameNumber + 1) % self.TotalAnimatedFrames
5973 image = self.AnimatedFrames[self.CurrentFrameNumber]
5974 try: # needed in case the window was closed with an "X"
5975 self.tktext_label.configure(image=image, width=image.width(), heigh=image.height())
5976 except Exception as e:
5977 print('Exception in update_animation', e)
5978 done = None
5979 return not done
5980

Callers 4

mainFunction · 0.80
mainFunction · 0.80
popup_animatedFunction · 0.80

Calls

no outgoing calls

Tested by

no test coverage detected