Advertisement
here2share

# Tk_gradient_frame.py

Apr 5th, 2021
940
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.51 KB | None | 0 0
  1. # Tk_gradient_frame.py
  2.  
  3. try:
  4.     from Tkinter import Canvas
  5.     from Tkconstants import *
  6. except ImportError:
  7.     from tkinter import Canvas
  8.     from tkinter.constants import *
  9.  
  10. from PIL import Image, ImageDraw, ImageTk
  11.  
  12. # Python 2/3 compatibility
  13. try:
  14.   basestring
  15. except NameError:
  16.   basestring = str
  17.  
  18. def hex2rgb(str_rgb):
  19.     try:
  20.         rgb = str_rgb[1:]
  21.  
  22.         if len(rgb) == 6:
  23.             r, g, b = rgb[0:2], rgb[2:4], rgb[4:6]
  24.         elif len(rgb) == 3:
  25.             r, g, b = rgb[0] * 2, rgb[1] * 2, rgb[2] * 2
  26.         else:
  27.             raise ValueError()
  28.     except:
  29.         raise ValueError("Invalid value %r provided for rgb color."% str_rgb)
  30.  
  31.     return tuple(int(v, 16) for v in (r, g, b))
  32.  
  33. class GradientFrame(Canvas):
  34.  
  35.     def __init__(self, master, from_color, to_color, width=None, height=None, orient=HORIZONTAL, steps=None, **kwargs):
  36.         Canvas.__init__(self, master, **kwargs)
  37.         if steps is None:
  38.             if orient == HORIZONTAL:
  39.                 steps = height
  40.             else:
  41.                 steps = width
  42.  
  43.         if isinstance(from_color, basestring):
  44.             from_color = hex2rgb(from_color)
  45.            
  46.         if isinstance(to_color, basestring):
  47.             to_color = hex2rgb(to_color)
  48.  
  49.         r,g,b = from_color
  50.         dr = float(to_color[0] - r)/steps
  51.         dg = float(to_color[1] - g)/steps
  52.         db = float(to_color[2] - b)/steps
  53.  
  54.         if orient == HORIZONTAL:
  55.             if height is None:
  56.                 raise ValueError("height can not be None")
  57.            
  58.             self.configure(height=height)
  59.            
  60.             if width is not None:
  61.                 self.configure(width=width)
  62.  
  63.             img_height = height
  64.             img_width = self.winfo_screenwidth()
  65.  
  66.             image = Image.new("RGB", (img_width, img_height), "#FFFFFF")
  67.             draw = ImageDraw.Draw(image)
  68.  
  69.             for i in range(steps):
  70.                 r,g,b = r+dr, g+dg, b+db
  71.                 y0 = int(float(img_height * i)/steps)
  72.                 y1 = int(float(img_height * (i+1))/steps)
  73.  
  74.                 draw.rectangle((0, y0, img_width, y1), fill=(int(r),int(g),int(b)))
  75.         else:
  76.             if width is None:
  77.                 raise ValueError("width can not be None")
  78.             self.configure(width=width)
  79.            
  80.             if height is not None:
  81.                 self.configure(height=height)
  82.  
  83.             img_height = self.winfo_screenheight()
  84.             img_width = width
  85.            
  86.             image = Image.new("RGB", (img_width, img_height), "#FFFFFF")
  87.             draw = ImageDraw.Draw(image)
  88.  
  89.             for i in range(steps):
  90.                 r,g,b = r+dr, g+dg, b+db
  91.                 x0 = int(float(img_width * i)/steps)
  92.                 x1 = int(float(img_width * (i+1))/steps)
  93.  
  94.                 draw.rectangle((x0, 0, x1, img_height), fill=(int(r),int(g),int(b)))
  95.        
  96.         self._gradient_photoimage = ImageTk.PhotoImage(image)
  97.  
  98.         self.create_image(0, 0, anchor=NW, image=self._gradient_photoimage)
  99.  
  100. if __name__ == "__main__":
  101.     try:
  102.         from Tkinter import Tk, Label
  103.     except ImportError:
  104.         from tkinter import Tk, Label
  105.  
  106.     root = Tk()
  107.  
  108.     Label(root, text="Gradient 1:").pack(anchor=W)
  109.     gfr = GradientFrame(root, from_color="#000000", to_color="#E74C3C", height=100)
  110.     gfr.pack(fill=X)
  111.  
  112.     Label(root, text="Gradient 2 (GTK gradient):").pack(anchor=W, pady=(20,0))
  113.     gfr = GradientFrame(root, from_color="#0000FF", to_color="#FFFF00", height=300)
  114.     gfr.pack(fill=X)
  115.      
  116.  
  117.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement