here2share

# tk_static_text_magic.py

Mar 16th, 2026 (edited)
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. # tk_static_text_magic.py
  2.  
  3. import tkinter as tk
  4.  
  5. R = "#ff0000"
  6. G = "#00ff00"
  7. B = "#0000ff"
  8.  
  9. ORANGE = "#ff7f00"
  10. YELLOW = "#ffff00"
  11. INDIGO = "#6b0086"
  12. PURPLE = "#cc00d3"
  13.  
  14. root = tk.Tk()
  15. root.geometry("1200x600+0+0")
  16. root.title("RAINBOW — Static Text Magic")
  17.  
  18. canvas = tk.Canvas(root, bg="black")
  19. canvas.pack(fill="both", expand=True)
  20.  
  21. font_size = 160
  22. font_family = "Franklin Gothic Heavy"
  23.  
  24. word = "RAINBOW"
  25. font = (font_family, font_size, "bold")
  26.  
  27. # Measure each character width using Tk's font engine
  28. fontname = canvas.tk.call(
  29.     "font", "create",
  30.     "-family", font_family,
  31.     "-size", font_size,
  32.     "-weight", "bold"
  33. )
  34.  
  35. char_widths = [int(canvas.tk.call("font", "measure", fontname, ch))
  36.                for ch in word]
  37.  
  38. char_widths = [
  39.     w - (0, 0, 24, 28, 29, 9, 32)[word.index(ch)
  40. ]
  41.     for ch, w in zip(word, char_widths)
  42. ]
  43.  
  44. total_width = sum(char_widths)
  45.  
  46. start_x = 600 - total_width // 2
  47. y = 300
  48.  
  49. x = start_x
  50. for ch, w in zip(word, char_widths):
  51.     canvas.create_text(
  52.         x + w // 2, y,
  53.         text=ch,
  54.         font=font,
  55.         fill="white",
  56.         tags=ch
  57.     )
  58.     x += w
  59.  
  60. canvas.itemconfig("R", fill=R)          # Red
  61. canvas.itemconfig("A", fill=ORANGE)     # Orange
  62. canvas.itemconfig("I", fill=YELLOW)     # Yellow
  63. canvas.itemconfig("N", fill=G)          # Green
  64. canvas.itemconfig("B", fill=B)          # Blue
  65. canvas.itemconfig("O", fill=INDIGO)     # Indigo
  66. canvas.itemconfig("W", fill=PURPLE)     # Violet
  67.  
  68. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment