Skip to content

Commit eb70737

Browse files
authored
Create Caesar-Cipher.py
1 parent 1ea6d03 commit eb70737

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
import sys
2+
import os
3+
import tkinter as tk
4+
from tkinter import messagebox
5+
6+
# =========================
7+
# THEME
8+
# =========================
9+
APP_BG = "#121212"
10+
PANEL_BG = "#1F1F1F"
11+
BTN_BG = "#2C2C2C"
12+
ACCENT = "#FF6F61"
13+
TEXT_CLR = "#E0E0E0"
14+
SUBTEXT_CLR = "#AAAAAA"
15+
16+
FONT = ("Segoe UI", 11)
17+
18+
# =========================
19+
# APP
20+
# =========================
21+
class CaesarCipherApp:
22+
def __init__(self, root):
23+
self.root = root
24+
root.title("MateTools – Caesar Cipher")
25+
root.geometry("800x500")
26+
root.configure(bg=APP_BG)
27+
root.resizable(False, False)
28+
29+
# =========================
30+
# LEFT PANEL
31+
# =========================
32+
left = tk.Frame(root, bg=PANEL_BG, width=350)
33+
left.pack(side="left", fill="y")
34+
35+
header = tk.Frame(left, bg=PANEL_BG)
36+
header.pack(fill="x", padx=16, pady=(18, 10))
37+
38+
tk.Label(
39+
header,
40+
text="MateTools",
41+
bg=PANEL_BG,
42+
fg=ACCENT,
43+
font=("Segoe UI", 20, "bold")
44+
).pack(side="left")
45+
46+
tk.Frame(left, bg=ACCENT, height=2).pack(fill="x", padx=16, pady=(0, 14))
47+
48+
tk.Label(
49+
left,
50+
text="Caesar Cipher Tool",
51+
bg=PANEL_BG,
52+
fg=TEXT_CLR,
53+
font=("Segoe UI", 14, "bold")
54+
).pack(anchor="w", padx=16, pady=(0, 2))
55+
56+
tk.Label(
57+
left,
58+
text="Encrypt or decrypt text using Caesar cipher",
59+
bg=PANEL_BG,
60+
fg=SUBTEXT_CLR,
61+
font=("Segoe UI", 10)
62+
).pack(anchor="w", padx=16, pady=(0, 16))
63+
64+
tk.Frame(left, bg=BTN_BG, height=1).pack(fill="x", padx=16, pady=(0, 16))
65+
66+
# =========================
67+
# INPUT
68+
# =========================
69+
tk.Label(left, text="Enter Text:", bg=PANEL_BG, fg=TEXT_CLR, font=FONT).pack(anchor="w", padx=16)
70+
self.input_text = tk.Text(left, height=5, width=40, bg="#1E1E1E", fg=TEXT_CLR, font=FONT)
71+
self.input_text.pack(padx=16, pady=(0, 10))
72+
73+
tk.Label(left, text="Shift:", bg=PANEL_BG, fg=TEXT_CLR, font=FONT).pack(anchor="w", padx=16)
74+
self.shift_var = tk.IntVar(value=3)
75+
tk.Entry(left, textvariable=self.shift_var, bg="#1E1E1E", fg=TEXT_CLR, font=FONT, width=5).pack(padx=16, pady=(0, 16))
76+
77+
# =========================
78+
# BUTTONS
79+
# =========================
80+
btn_frame = tk.Frame(left, bg=PANEL_BG)
81+
btn_frame.pack(fill="x", padx=16, pady=16)
82+
83+
def make_btn(text, cmd, color=BTN_BG):
84+
return tk.Button(
85+
btn_frame,
86+
text=text,
87+
command=cmd,
88+
bg=color,
89+
fg="white",
90+
font=("Segoe UI", 11, "bold"),
91+
relief="flat",
92+
height=2,
93+
width=18
94+
)
95+
96+
make_btn("Encrypt", self.encrypt_text, ACCENT).pack(side="left", expand=True, padx=4)
97+
make_btn("Decrypt", self.decrypt_text, "#4CAF50").pack(side="left", expand=True, padx=4)
98+
99+
# =========================
100+
# RIGHT PANEL
101+
# =========================
102+
right = tk.Frame(root, bg=APP_BG)
103+
right.pack(side="right", fill="both", expand=True)
104+
105+
self.output_card = tk.Frame(right, bg=PANEL_BG)
106+
self.output_card.pack(padx=30, pady=40, fill="both", expand=True)
107+
108+
tk.Label(
109+
self.output_card,
110+
text="Output",
111+
bg=PANEL_BG,
112+
fg=TEXT_CLR,
113+
font=("Segoe UI", 14, "bold")
114+
).pack(pady=(20, 10))
115+
116+
self.output_text = tk.Text(self.output_card, height=10, bg="#1E1E1E", fg=TEXT_CLR, font=FONT)
117+
self.output_text.pack(padx=16, pady=10, fill="both", expand=True)
118+
119+
# =========================
120+
# METHODS
121+
# =========================
122+
def encrypt_text(self):
123+
text = self.input_text.get("1.0", tk.END).strip()
124+
shift = self.shift_var.get()
125+
self.output_text.delete("1.0", tk.END)
126+
self.output_text.insert(tk.END, self.caesar_cipher(text, shift))
127+
128+
def decrypt_text(self):
129+
text = self.input_text.get("1.0", tk.END).strip()
130+
shift = self.shift_var.get()
131+
self.output_text.delete("1.0", tk.END)
132+
self.output_text.insert(tk.END, self.caesar_cipher(text, -shift))
133+
134+
def caesar_cipher(self, text, shift):
135+
result = ""
136+
for char in text:
137+
if char.isupper():
138+
result += chr((ord(char) - 65 + shift) % 26 + 65)
139+
elif char.islower():
140+
result += chr((ord(char) - 97 + shift) % 26 + 97)
141+
else:
142+
result += char
143+
return result
144+
145+
# =========================
146+
# RUN
147+
# =========================
148+
if __name__ == "__main__":
149+
root = tk.Tk()
150+
CaesarCipherApp(root)
151+
root.mainloop()

0 commit comments

Comments
 (0)