Advertisement
here2share

# tk_radial_text.py

Nov 29th, 2023 (edited)
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. # tk_radial_text.py
  2.  
  3. import tkinter as tk
  4. from PIL import ImageTk, ImageDraw, ImageFont, Image
  5. from tkinter import ttk, font, colorchooser
  6. import math
  7.  
  8. ww = 540
  9. hh = 540
  10.  
  11. root = tk.Tk()
  12. root.title("Radial Text Editor")
  13. root.geometry('{}x{}'.format(ww, hh))
  14.  
  15. canvas = tk.Canvas(width=ww, height=hh, bg='white')
  16. canvas.pack()
  17.  
  18. text_color = "red"
  19.  
  20. radius = 250
  21. width_target = int(2 * math.pi * radius)
  22.  
  23. # default text
  24. text = "Hello, World! This is Python..."
  25.  
  26. font_families = []
  27. for ttt in font.families():
  28.     try:
  29.         ImageFont.truetype(ttt.lower(), 9)
  30.         font_families += [ttt]
  31.     except:
  32.         0
  33. '''
  34. for font_family in font_families:
  35.    print(font_family)
  36. '''
  37.  
  38. def reshape_text_to_circle(text, color):
  39.     global font_family
  40.     try:
  41.         font_family = font_family_option.get()
  42.     except:
  43.         font_family = 'Verdana'
  44.     canvas.delete('all')
  45.     font_size = 9
  46.     image = Image.new('RGB', (width_target, radius - 20), 'white')
  47.     while True:
  48.         font = ImageFont.truetype(font_family.lower(), font_size)
  49.         text_width, text_height = ImageDraw.Draw(image).textsize(text+' ', font=font)
  50.         if text_width >= width_target:
  51.             break
  52.         font_size += 1
  53.  
  54.     image = Image.new('RGB', (width_target, text_height), 'white')
  55.     text_buffer = ImageDraw.Draw(image)
  56.     text_buffer.text((0, -10), text, fill=color, font=font)
  57.     # image.show(text_buffer)
  58.  
  59.     canvas_image = Image.new('RGB', (ww, hh), 'white')
  60.     text_image = ImageDraw.Draw(canvas_image)
  61.  
  62.     angle_incr = 360 / width_target
  63.     xOffset = 0
  64.     yOffset = 0
  65.     for y in range(text_height):
  66.         distance = 250 - y + yOffset
  67.         for x in range(width_target):
  68.             rgb = image.getpixel((x, y))
  69.             if sum(rgb) < 3 * 255:
  70.                 if not yOffset:
  71.                     yOffset = y
  72.                 angle = x * angle_incr - 180
  73.                 x_coord = int(distance * math.cos(math.radians(angle)) + ww/2)
  74.                 y_coord = int(distance * math.sin(math.radians(angle)) + hh/2)
  75.                 text_image.point((x_coord, y_coord), "#%02x%02x%02x" % rgb)
  76.  
  77.     # canvas_image.show()
  78.    
  79.     photo = ImageTk.PhotoImage(canvas_image)
  80.     canvas.create_image(0, 0, anchor='nw', image=photo)
  81.     canvas.image = photo
  82.     canvas.update()
  83.  
  84.  
  85. def update_text():
  86.     global text
  87.     text = text_entry.get()
  88.     reshape_text_to_circle(text, text_color)
  89.  
  90. def change_text_color():
  91.     global text_color
  92.     color = colorchooser.askcolor(title="Select Color")[1]
  93.     if color:
  94.         text_color = color
  95.         reshape_text_to_circle(text, text_color)
  96.  
  97. def show_floating_window():
  98.     global text_entry, font_family_option, update_button, color_button, floating_window
  99.  
  100.     try:
  101.         floating_window.destroy()
  102.     except:
  103.         0
  104.  
  105.     floating_window = tk.Toplevel(root)
  106.     floating_window.title("Radial Text Editor")
  107.  
  108.     text_label = tk.Label(floating_window, text="Text:")
  109.     text_label.pack()
  110.  
  111.     text_entry = tk.Entry(floating_window)
  112.     text_entry.pack()
  113.     text_entry.insert(0, text)
  114.  
  115.     font_family_label = tk.Label(floating_window, text="Font Family:")
  116.     font_family_label.pack()
  117.  
  118.     font_family_option = ttk.Combobox(floating_window, values=font_families, state="readonly")
  119.     font_family_option.pack()
  120.     font_family_option.set(font_family)
  121.  
  122.     color_button = tk.Button(floating_window, text="Change Color", command=change_text_color)
  123.     color_button.pack()
  124.  
  125.     update_button = tk.Button(floating_window, text="Update", command=update_text, bg='lime')
  126.     update_button.pack()
  127.  
  128. menubar = tk.Menu(root)
  129. editor_menu = tk.Menu(menubar, tearoff=0)
  130. editor_menu.add_command(label="Show Editor", command=show_floating_window)
  131. menubar.add_cascade(label="Menu", menu=editor_menu)
  132. root.config(menu=menubar)
  133.  
  134. reshape_text_to_circle(text, text_color)
  135.  
  136. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement