import tkinter as tk
import random
import json
import os
import threading
from wordfreq import top_n_list
from deep_translator import GoogleTranslator
========================= #
CONFIG
=========================
LANGS = ["es", "ja", "fr", "de"]
TOP_N = 5000
CYCLE_MS = 1000
CACHE_FILE = "translation_cache.json"
========================= #
LOAD WORDS
=========================
words = top_n_list("en", TOP_N)
========================= #
LOAD CACHE
=========================
if os.path.exists(CACHE_FILE):
with open(CACHE_FILE, "r", encoding="utf-8") as f:
translator_cache = json.load(f)
else:
translator_cache = {}
def save_cache():
with open(CACHE_FILE, "w", encoding="utf-8") as f:
json.dump(translator_cache, f, ensure_ascii=False)
def get_translations_async(word, callback):
"""Fetches translations in a background thread to prevent GUI freezing."""
def worker():
lines = [word.upper()]
cache_updated = False
for lang in LANGS:
key = f"{word}_{lang}"
if key in translator_cache:
lines.append(translator_cache[key])
else:
try:
result = GoogleTranslator(source="en", target=lang).translate(word)
translator_cache[key] = result
cache_updated = True
lines.append(result)
except Exception:
lines.append("?")
if cache_updated:
save_cache()
root.after(0, callback, lines)
threading.Thread(target=worker, daemon=True).start()
========================= #
GUI
=========================
root = tk.Tk()
root.attributes("-fullscreen", True)
root.configure(bg="black")
label = tk.Label(
root,
fg="white",
bg="black",
font=("Arial", 48, "bold"),
justify="center"
)
label.pack(expand=True)
speed_label = tk.Label(root, fg="gray", bg="black", font=("Arial", 14))
speed_label.pack(side="bottom", pady=20)
========================= #
CONTINUOUS REPLACEMENT
=========================
current_timer = None
def update_display(lines):
label.config(text="\n\n".join(lines))
schedule_next_cycle()
def cycle():
word = random.choice(words)
get_translations_async(word, update_display)
def schedule_next_cycle():
global current_timer
current_timer = root.after(CYCLE_MS, cycle)
========================= #
SPEED AND SYSTEM CONTROL
=========================
def update_speed_ui():
speed_label.config(text=f"Interval: {CYCLE_MS}ms")
def change_speed(amount):
global CYCLE_MS, current_timer
CYCLE_MS = max(200, CYCLE_MS + amount)
update_speed_ui()
if current_timer:
root.after_cancel(current_timer)
schedule_next_cycle()
Bindings (Angled brackets removed for YouTube description compatibility)
root.bind("Up", lambda e: change_speed(-100)) # Faster
root.bind("Down", lambda e: change_speed(100)) # Slower
root.bind("Escape", lambda e: root.destroy()) # Close app
========================= #
INIT
=========================
update_speed_ui()
cycle()
root.mainloop()