Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_radial_text.py
- import tkinter as tk
- from PIL import ImageTk, ImageDraw, ImageFont, Image
- from tkinter import ttk, font, colorchooser
- import math
- ww = 540
- hh = 540
- root = tk.Tk()
- root.title("Radial Text Editor")
- root.geometry('{}x{}'.format(ww, hh))
- canvas = tk.Canvas(width=ww, height=hh, bg='white')
- canvas.pack()
- text_color = "red"
- radius = 250
- width_target = int(2 * math.pi * radius)
- # default text
- text = "Hello, World! This is Python..."
- font_families = []
- for ttt in font.families():
- try:
- ImageFont.truetype(ttt.lower(), 9)
- font_families += [ttt]
- except:
- 0
- '''
- for font_family in font_families:
- print(font_family)
- '''
- def reshape_text_to_circle(text, color):
- global font_family
- try:
- font_family = font_family_option.get()
- except:
- font_family = 'Verdana'
- canvas.delete('all')
- font_size = 9
- image = Image.new('RGB', (width_target, radius - 20), 'white')
- while True:
- font = ImageFont.truetype(font_family.lower(), font_size)
- text_width, text_height = ImageDraw.Draw(image).textsize(text+' ', font=font)
- if text_width >= width_target:
- break
- font_size += 1
- image = Image.new('RGB', (width_target, text_height), 'white')
- text_buffer = ImageDraw.Draw(image)
- text_buffer.text((0, -10), text, fill=color, font=font)
- # image.show(text_buffer)
- canvas_image = Image.new('RGB', (ww, hh), 'white')
- text_image = ImageDraw.Draw(canvas_image)
- angle_incr = 360 / width_target
- xOffset = 0
- yOffset = 0
- for y in range(text_height):
- distance = 250 - y + yOffset
- for x in range(width_target):
- rgb = image.getpixel((x, y))
- if sum(rgb) < 3 * 255:
- if not yOffset:
- yOffset = y
- angle = x * angle_incr - 180
- x_coord = int(distance * math.cos(math.radians(angle)) + ww/2)
- y_coord = int(distance * math.sin(math.radians(angle)) + hh/2)
- text_image.point((x_coord, y_coord), "#%02x%02x%02x" % rgb)
- # canvas_image.show()
- photo = ImageTk.PhotoImage(canvas_image)
- canvas.create_image(0, 0, anchor='nw', image=photo)
- canvas.image = photo
- canvas.update()
- def update_text():
- global text
- text = text_entry.get()
- reshape_text_to_circle(text, text_color)
- def change_text_color():
- global text_color
- color = colorchooser.askcolor(title="Select Color")[1]
- if color:
- text_color = color
- reshape_text_to_circle(text, text_color)
- def show_floating_window():
- global text_entry, font_family_option, update_button, color_button, floating_window
- try:
- floating_window.destroy()
- except:
- 0
- floating_window = tk.Toplevel(root)
- floating_window.title("Radial Text Editor")
- text_label = tk.Label(floating_window, text="Text:")
- text_label.pack()
- text_entry = tk.Entry(floating_window)
- text_entry.pack()
- text_entry.insert(0, text)
- font_family_label = tk.Label(floating_window, text="Font Family:")
- font_family_label.pack()
- font_family_option = ttk.Combobox(floating_window, values=font_families, state="readonly")
- font_family_option.pack()
- font_family_option.set(font_family)
- color_button = tk.Button(floating_window, text="Change Color", command=change_text_color)
- color_button.pack()
- update_button = tk.Button(floating_window, text="Update", command=update_text, bg='lime')
- update_button.pack()
- menubar = tk.Menu(root)
- editor_menu = tk.Menu(menubar, tearoff=0)
- editor_menu.add_command(label="Show Editor", command=show_floating_window)
- menubar.add_cascade(label="Menu", menu=editor_menu)
- root.config(menu=menubar)
- reshape_text_to_circle(text, text_color)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement