import random import sys from io import StringIO from tkinter import END, Button, Text, Tk, TclError class Greetings: GREET = ( "Bonne journée", "Добрый день", "良い一日", "Seid gegrüßt 🫂", "Guten Abend 🌙", "Hallo ❤️‍🔥", ) def __init__(self): self.greetings = self._sample() def _sample(self): return random.sample(self.GREET, k=len(self.GREET)) def __call__(self): if not self.greetings: self.greetings = self._sample() return self.greetings.pop() def blocks(namen, greetings, file=None): if file is None: file = sys.stdout namen = sorted(namen, key=str.lower) blk = 1 done = False current = greetings() while namen: if len(current) + len(namen[0]) + 2 < 200: current += f" @{namen.pop(0)}" done = False else: # print(f"[{blk}]", file=file) print(current, file=file) blk += 1 current = greetings() print(file=file) done = True if not done: #print(f"[{blk}]", file=file) print(current, file=file) return file def strip_names(lines): return [stripped for line in lines if (stripped := line.strip())] class GUI(Tk): def __init__(self): super().__init__() self.text = Text(self, height=18, width=80) self.text.pack() self.insert_text() Button(self, text="Generate", command=self.text_generate).pack() Button(self, text="Clear", command=self.text_delete).pack() Button(self, text="Close", command=self.destroy).pack() def insert_text(self): try: if text := self.text.clipboard_get().strip(): self.text.insert("1.0", text) self.text_generate() except TclError: pass def text_delete(self): self.text.delete("1.0", END) def text_generate(self): s = self.text.get("1.0", END) self.text.delete("1.0", END) io = StringIO() blocks(strip_names(s.splitlines()), Greetings(), io) self.text.insert("1.0", io.getvalue()) self.text.clipboard_append(s) if __name__ == "__main__": GUI().mainloop()