Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # tk_static_text_magic.py
- import tkinter as tk
- R = "#ff0000"
- G = "#00ff00"
- B = "#0000ff"
- ORANGE = "#ff7f00"
- YELLOW = "#ffff00"
- INDIGO = "#6b0086"
- PURPLE = "#cc00d3"
- root = tk.Tk()
- root.geometry("1200x600+0+0")
- root.title("RAINBOW — Static Text Magic")
- canvas = tk.Canvas(root, bg="black")
- canvas.pack(fill="both", expand=True)
- font_size = 160
- font_family = "Franklin Gothic Heavy"
- word = "RAINBOW"
- font = (font_family, font_size, "bold")
- # Measure each character width using Tk's font engine
- fontname = canvas.tk.call(
- "font", "create",
- "-family", font_family,
- "-size", font_size,
- "-weight", "bold"
- )
- char_widths = [int(canvas.tk.call("font", "measure", fontname, ch))
- for ch in word]
- char_widths = [
- w - (0, 0, 24, 28, 29, 9, 32)[word.index(ch)
- ]
- for ch, w in zip(word, char_widths)
- ]
- total_width = sum(char_widths)
- start_x = 600 - total_width // 2
- y = 300
- x = start_x
- for ch, w in zip(word, char_widths):
- canvas.create_text(
- x + w // 2, y,
- text=ch,
- font=font,
- fill="white",
- tags=ch
- )
- x += w
- canvas.itemconfig("R", fill=R) # Red
- canvas.itemconfig("A", fill=ORANGE) # Orange
- canvas.itemconfig("I", fill=YELLOW) # Yellow
- canvas.itemconfig("N", fill=G) # Green
- canvas.itemconfig("B", fill=B) # Blue
- canvas.itemconfig("O", fill=INDIGO) # Indigo
- canvas.itemconfig("W", fill=PURPLE) # Violet
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment