|
1 | 1 | import tkinter as tk
|
2 | 2 | from tkinter import ttk
|
3 |
| - |
4 | 3 | import random
|
5 | 4 |
|
6 | 5 |
|
7 |
| -def generate_charset(seed=357): |
| 6 | +def generate_charset(seed=None): |
8 | 7 | if seed is not None:
|
9 | 8 | random.seed(seed)
|
| 9 | + charset = list( |
| 10 | + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 11 | + "abcdefghijklmnopqrstuvwxyz" |
| 12 | + "0123456789" |
| 13 | + "!@#$%^&*()-_=+[]{}|;:',.<>?/`~\"\\" |
| 14 | + "“”‘’" |
| 15 | + ) |
| 16 | + random.shuffle(charset) |
| 17 | + return ''.join(charset) |
| 18 | + |
| 19 | + |
| 20 | +def encode(text: str, charset: str, shift: int) -> str: |
| 21 | + result = "" |
| 22 | + for char in text: |
| 23 | + if char in charset: |
| 24 | + index = charset.index(char) |
| 25 | + result += charset[(index + shift) % len(charset)] |
| 26 | + else: |
| 27 | + result += char |
| 28 | + return result |
10 | 29 |
|
11 |
| - alpha = list("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz") |
12 |
| - random.shuffle(alpha) |
13 | 30 |
|
14 |
| - extra = list("0123456789!@#$%^&*()-_=+[]{}|;:',.<>?/`~\"\\") |
15 |
| - return "".join(alpha + extra) |
| 31 | +def decode(text: str, charset: str, shift: int) -> str: |
| 32 | + return encode(text, charset, -shift) |
16 | 33 |
|
17 | 34 |
|
18 |
| -# Then in your encoder: |
19 |
| -CHARSET = generate_charset() |
| 35 | +class EncoderDecoderUI(): |
| 36 | + def __init__(self, seed: int, shift: int): |
| 37 | + # --- GUI setup --- |
| 38 | + self.root = tk.Tk() |
| 39 | + self.root.title("Text Encoder/Decoder with Seed") |
| 40 | + self.root.geometry("600x300") |
| 41 | + self.root.resizable(True, True) |
20 | 42 |
|
| 43 | + # --- Configure grid to be resizable --- |
| 44 | + for col in range(4): |
| 45 | + self.root.columnconfigure(col, weight=1) |
| 46 | + self.root.rowconfigure(1, weight=1) |
| 47 | + self.root.rowconfigure(5, weight=1) |
21 | 48 |
|
22 |
| -def encode(text: str, shift: int) -> str: |
23 |
| - result = "" |
24 |
| - for char in text: |
25 |
| - if char in CHARSET: |
26 |
| - index = CHARSET.index(char) |
27 |
| - result += CHARSET[(index + shift) % len(CHARSET)] |
28 |
| - else: |
29 |
| - result += char # Leave unsupported characters unchanged |
30 |
| - return result |
| 49 | + # --- Widgets --- |
| 50 | + ttk.Label(self.root, text="Input Text:").grid(row=0, column=0, padx=5, pady=5, sticky="w") |
| 51 | + |
| 52 | + self.input_text = tk.Text(self.root, height=5) |
| 53 | + self.input_text.grid(row=1, column=0, columnspan=4, padx=5, pady=5, sticky="nsew") |
| 54 | + |
| 55 | + ttk.Label(self.root, text="Shift:").grid(row=2, column=0, padx=5, sticky="e") |
| 56 | + self.shift_entry = ttk.Entry(self.root, width=5) |
| 57 | + self.shift_entry.insert(0, str(shift)) |
| 58 | + self.shift_entry.grid(row=2, column=1, padx=5, sticky="w") |
| 59 | + |
| 60 | + ttk.Label(self.root, text="Seed:").grid(row=2, column=2, padx=5, sticky="e") |
| 61 | + self.seed_entry = ttk.Entry(self.root, width=10) |
| 62 | + self.seed_entry.insert(0, str(seed)) |
| 63 | + self.seed_entry.grid(row=2, column=3, padx=5, sticky="w") |
| 64 | + |
| 65 | + ttk.Button(self.root, text="Encode", command=self.on_encode).grid(row=3, column=1, sticky="w", padx=5, pady=5) |
| 66 | + ttk.Button(self.root, text="Decode", command=self.on_decode).grid(row=3, column=2, sticky="e", padx=5, pady=5) |
31 | 67 |
|
32 |
| -def decode(text: str, shift: int) -> str: |
33 |
| - return encode(text, -shift) |
| 68 | + ttk.Label(self.root, text="Output Text:").grid(row=4, column=0, padx=5, pady=5, sticky="w") |
34 | 69 |
|
| 70 | + self.output_text = tk.Text(self.root, height=5) |
| 71 | + self.output_text.grid(row=5, column=0, columnspan=4, padx=5, pady=5, sticky="nsew") |
35 | 72 |
|
36 |
| -def create_ui(): |
37 |
| - def on_encode(): |
38 |
| - shift = int(shift_entry.get()) |
39 |
| - text = input_text.get("1.0", tk.END).rstrip('\n') |
40 |
| - output_text.delete("1.0", tk.END) |
41 |
| - output_text.insert(tk.END, encode(text, shift)) |
| 73 | + self.root.mainloop() |
42 | 74 |
|
43 |
| - def on_decode(): |
44 |
| - shift = int(shift_entry.get()) |
45 |
| - text = input_text.get("1.0", tk.END).rstrip('\n') |
46 |
| - output_text.delete("1.0", tk.END) |
47 |
| - output_text.insert(tk.END, decode(text, shift)) |
48 | 75 |
|
49 |
| - # GUI setup |
50 |
| - root = tk.Tk() |
51 |
| - root.title("Simple Text Encoder/Decoder") |
| 76 | + def get_seed_and_shift(self): |
| 77 | + try: |
| 78 | + shift = int(self.shift_entry.get()) |
| 79 | + seed = int(self.seed_entry.get()) |
| 80 | + except ValueError: |
| 81 | + self.output_text.delete("1.0", tk.END) |
| 82 | + self.output_text.insert(tk.END, "Error: Shift and Seed must be integers.") |
| 83 | + return seed, shift |
52 | 84 |
|
53 |
| - ttk.Label(root, text="Input Text:").grid(row=0, column=0, padx=5, pady=5, sticky="w") |
54 |
| - input_text = tk.Text(root, height=5, width=60) |
55 |
| - input_text.grid(row=1, column=0, columnspan=3, padx=5, pady=5) |
56 | 85 |
|
57 |
| - ttk.Label(root, text="Shift:").grid(row=2, column=0, padx=5, sticky="e") |
58 |
| - shift_entry = ttk.Entry(root, width=5) |
59 |
| - shift_entry.insert(0, "8") |
60 |
| - shift_entry.grid(row=2, column=1, padx=5, sticky="w") |
| 86 | + def on_encode(self): |
| 87 | + shift, seed = self.get_seed_and_shift() |
| 88 | + charset = generate_charset(seed) |
| 89 | + text = self.input_text.get("1.0", tk.END).rstrip('\n') |
| 90 | + self.output_text.delete("1.0", tk.END) |
| 91 | + self.output_text.insert(tk.END, encode(text, charset, shift)) |
61 | 92 |
|
62 |
| - ttk.Button(root, text="Encode", command=on_encode).grid(row=2, column=2, sticky="w", padx=5) |
63 |
| - ttk.Button(root, text="Decode", command=on_decode).grid(row=2, column=2, sticky="e", padx=5) |
64 | 93 |
|
65 |
| - ttk.Label(root, text="Output Text:").grid(row=3, column=0, padx=5, pady=5, sticky="w") |
66 |
| - output_text = tk.Text(root, height=5, width=60) |
67 |
| - output_text.grid(row=4, column=0, columnspan=3, padx=5, pady=5) |
| 94 | + def on_decode(self): |
| 95 | + shift, seed = self.get_seed_and_shift() |
| 96 | + charset = generate_charset(seed) |
| 97 | + text = self.input_text.get("1.0", tk.END).rstrip('\n') |
| 98 | + self.output_text.delete("1.0", tk.END) |
| 99 | + self.output_text.insert(tk.END, decode(text, charset, shift)) |
68 | 100 |
|
69 |
| - root.mainloop() |
70 | 101 |
|
71 | 102 | if __name__ == "__main__":
|
72 |
| - create_ui() |
| 103 | + EncoderDecoderUI(357, 8) |
0 commit comments