Advertisement
here2share

# Tk_ani_Stacked_Frame.py

Apr 5th, 2021
768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.14 KB | None | 0 0
  1. # Tk_ani_Stacked_Frame.py
  2.  
  3. try:
  4.     from Tkinter import Frame, N, S, E, W
  5. except ImportError:
  6.     from tkinter import Frame, N, S, E, W
  7.  
  8. class Animation(object):
  9.     def __init__(self, w, ticks, config_function, duration=1, interval_time=None, easing_function=None, start_value=0, end_value=1, callback=None):
  10.         self._w = w
  11.  
  12.         self._tick = 0
  13.         self._total_ticks = float(ticks)
  14.        
  15.         if easing_function is None:
  16.             self._easing_function = lambda x: x
  17.  
  18.         self._duration = duration
  19.        
  20.         if interval_time:
  21.             self._interval_time = int(interval_time * 1000)
  22.         else:
  23.             self._interval_time = int(duration * 1000 / self._total_ticks)
  24.        
  25.         self._start_value = start_value
  26.         self._end_value = end_value
  27.         self._interval_value = end_value - start_value
  28.        
  29.         self._config_function = config_function
  30.        
  31.         self._callback = callback
  32.  
  33.     def start_animation(self, after=0):    
  34.         if after != 0:
  35.             self.after(int(after*1000), self._animate)
  36.         else:
  37.             self._animate()
  38.        
  39.     def _animate(self):
  40.         t =  self._tick / self._total_ticks
  41.  
  42.         value = self._start_value + self._interval_value * self._easing_function(t)
  43.         self._config_function(value)
  44.        
  45.         self._tick += 1
  46.        
  47.         if self._tick <= self._total_ticks:
  48.             self._w.after(self._interval_time, self._animate)
  49.         else:
  50.             if self._callback is not None:
  51.                 self._w.after(self._interval_time, self._callback)
  52.  
  53. class Stacked_Frame(Frame):
  54.     def __init__(self, master, animate=False, **kw):
  55.         Frame.__init__(self, master, **kw)
  56.         self._list_of_widgets = []
  57.        
  58.         self._current_index = None
  59.         self._current_widget = None
  60.        
  61.         self.animate_direction=N
  62.        
  63.         self._animate = animate
  64.                
  65.         self._is_animating = False
  66.  
  67.     def add_widget(self, widget):
  68.         self._list_of_widgets.append(widget)
  69.         if self._current_index is None:
  70.             self._current_index = 0
  71.             self._show_widget(widget)
  72.  
  73.         index = len(self._list_of_widgets) - 1
  74.         return index
  75.  
  76.     def remove_widget(self, widget):
  77.         self._list_of_widgets.remove(widget)
  78.  
  79.     def insert_widget(self, index, widget):
  80.         self._list_of_widgets.insert(index, widget)
  81.         if self._current_index is None:
  82.             self._current_index = 0
  83.  
  84.             self._show_widget(widget)
  85.        
  86.     def count(self):
  87.         return len(self._list_of_widgets)
  88.  
  89.     def current_index(self):
  90.         return self._current_index
  91.  
  92.     def index_of(self, widget):
  93.         return self._list_of_widgets.index(widget)
  94.  
  95.     def set_current_index(self, index):
  96.         if self._is_animating:
  97.             return
  98.  
  99.         if index == self._current_index: return
  100.        
  101.         widget = self._list_of_widgets[index]
  102.         self._current_index = index
  103.        
  104.         self._show_widget(widget)
  105.        
  106.     def set_current_widget(self, widget):
  107.         if self._is_animating:
  108.             return
  109.  
  110.         index = self._list_of_widgets.index(widget)
  111.         self._current_index = index
  112.  
  113.         self._show_widget(widget)
  114.        
  115.     def widget(self, index):
  116.         return self._list_of_widgets[index]
  117.    
  118.     def next(self):
  119.         if self._current_index  == len(self._list_of_widgets) - 1:
  120.             return
  121.            
  122.         if self._is_animating:
  123.             return
  124.  
  125.         self._current_index += 1
  126.         self.animate_direction=N
  127.         widget = self._list_of_widgets[self._current_index]
  128.        
  129.         self._show_widget(widget)
  130.  
  131.     def previous(self):
  132.         if not self._current_index:
  133.             return
  134.        
  135.         if self._is_animating:
  136.             return
  137.  
  138.         self._current_index -= 1
  139.         self.animate_direction=S
  140.         widget = self._list_of_widgets[self._current_index]
  141.        
  142.         self._show_widget(widget)
  143.        
  144.     def _show_widget(self, widget):
  145.         if self._current_widget is None:
  146.             self._current_widget = widget
  147.             widget.place(x=0, y=0, relwidth=1, relheight=1)
  148.         else:
  149.             if self._animate:
  150.                 old_widget = self._current_widget
  151.  
  152.                 widget.place(relwidth=1, relheight=1)
  153.  
  154.                 if self.animate_direction == W:
  155.                     start_value = 0
  156.                     end_value = self.winfo_width()
  157.  
  158.                     def config_function(position):
  159.                         widget.place(x=position, y=0, anchor=N+E)
  160.                         old_widget.place(x=position, y=0, anchor=N+W)
  161.  
  162.                 elif self.animate_direction == E:
  163.                     start_value = self.winfo_width()
  164.                     end_value = 0
  165.                    
  166.                     def config_function(position):
  167.                         widget.place(x=position, y=0, anchor=N+W)
  168.                         old_widget.place(x=position, y=0, anchor=N+E)
  169.  
  170.                 elif self.animate_direction == S:
  171.                     start_value = 0
  172.                     end_value = self.winfo_height()
  173.                    
  174.                     def config_function(position):
  175.                         widget.place(x=0, y=position, anchor=S+W)
  176.                         old_widget.place(x=0, y=position, anchor=N+W)
  177.  
  178.                 elif self.animate_direction == N:
  179.                     start_value = self.winfo_height()
  180.                     end_value = 0
  181.  
  182.                     def config_function(position):
  183.                         widget.place(x=0, y=position, anchor=N+W)
  184.                         old_widget.place(x=0, y=position, anchor=S+W)
  185.  
  186.                 animation = Animation(
  187.                     self,
  188.                     ticks=20,
  189.                     interval_time=0.05,
  190.                     start_value=start_value,
  191.                     end_value=end_value,
  192.                     config_function=config_function,
  193.                     callback=lambda widget=widget: self._on_finish_animation(widget))
  194.  
  195.                 animation.start_animation()
  196.                 self._is_animating = True
  197.             else:
  198.                 self._current_widget.place_forget()
  199.                 self._current_widget = widget
  200.            
  201.                 widget.place(x=0, y=0, relwidth=1, relheight=1)
  202.  
  203.     def _on_finish_animation(self, widget):
  204.         self._current_widget.place_forget()
  205.         self._current_widget = widget
  206.        
  207.         self._is_animating = False
  208.  
  209. if __name__ == "__main__":
  210.     try:
  211.         from Tkinter import Tk, Button, Label
  212.     except ImportError:
  213.         from tkinter import Tk, Button, Label
  214.        
  215.     root = Tk()
  216.     stack = Stacked_Frame(root, width=300, height=400, animate=True)
  217.     stack.pack(padx=5)
  218.    
  219.     frame1 = Frame(stack, background="red")
  220.     Label(frame1, text="this is frame1").pack(expand=True)
  221.  
  222.     frame2 = Frame(stack, background="white")
  223.     Label(frame2, text="this is frame2").pack(expand=True)
  224.    
  225.     frame3 = Frame(stack, background="yellow")
  226.     Label(frame3, text="this is frame3").pack(expand=True)
  227.    
  228.     frame4 = Frame(stack, background="green")
  229.     Label(frame4, text="this is frame4").pack(expand=True)
  230.  
  231.     stack.add_widget(frame1)
  232.     stack.add_widget(frame2)
  233.     stack.add_widget(frame3)
  234.     stack.add_widget(frame4)
  235.    
  236.     row = Frame(root)
  237.     row.pack(fill="x", pady=10, padx=5)
  238.     Button(row, text="previous", command= lambda: stack.previous()).pack(side="left")
  239.     Button(row, text="next", command= lambda: stack.next()).pack(side="left", padx=(8,0))
  240.  
  241.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement