Skip to content

Commit 36a6de5

Browse files
rename: snake_case file name
following standard python naming convention.
1 parent 03e9c40 commit 36a6de5

File tree

1 file changed

+263
-0
lines changed

1 file changed

+263
-0
lines changed

encrypter_decrypter_gui.py

+263
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,263 @@
1+
# ==================== Importing Libraries ====================
2+
# =============================================================
3+
import tkinter as tk
4+
from tkinter import ttk
5+
from tkinter.messagebox import showerror
6+
from tkinter.scrolledtext import ScrolledText
7+
8+
# =============================================================
9+
10+
11+
class Main(tk.Tk):
12+
def __init__(self, *args, **kwargs):
13+
super().__init__(*args, **kwargs)
14+
self.title("Alphacrypter")
15+
# ----- Setting Geometry -----
16+
self.geometry_settings()
17+
18+
def geometry_settings(self):
19+
_com_scr_w = self.winfo_screenwidth()
20+
_com_scr_h = self.winfo_screenheight()
21+
_my_w = 300
22+
_my_h = 450
23+
# ----- Now Getting X and Y Coordinates
24+
_x = int(_com_scr_w / 2 - _my_w / 2)
25+
_y = int(_com_scr_h / 2 - _my_h / 2)
26+
_geo_string = str(_my_w) + "x" + str(_my_h) + "+" + str(_x) + "+" + str(_y)
27+
self.geometry(_geo_string)
28+
# ----- Geometry Setting Completed Now Disabling Resize Screen Button -----
29+
self.resizable(width=False, height=False)
30+
31+
32+
class Notebook:
33+
def __init__(self, parent):
34+
self.parent = parent
35+
# ========== Data Key ==========
36+
self.data_dic = {
37+
"A": "Q",
38+
"B": "W",
39+
"C": "E",
40+
"D": "R",
41+
"E": "T",
42+
"F": "Y",
43+
"G": "U",
44+
"H": "I",
45+
"I": "O",
46+
"J": "P",
47+
"K": "A",
48+
"L": "S",
49+
"M": "D",
50+
"N": "F",
51+
"O": "G",
52+
"P": "H",
53+
"Q": "J",
54+
"R": "K",
55+
"S": "L",
56+
"T": "Z",
57+
"U": "X",
58+
"V": "C",
59+
"W": "V",
60+
"X": "B",
61+
"Y": "N",
62+
"Z": "M",
63+
"a": "q",
64+
"b": "w",
65+
"c": "e",
66+
"d": "r",
67+
"e": "t",
68+
"f": "y",
69+
"g": "u",
70+
"h": "i",
71+
"i": "o",
72+
"j": "p",
73+
"k": "a",
74+
"l": "s",
75+
"m": "d",
76+
"n": "f",
77+
"o": "g",
78+
"p": "h",
79+
"q": "j",
80+
"r": "k",
81+
"s": "l",
82+
"t": "z",
83+
"u": "x",
84+
"v": "c",
85+
"w": "v",
86+
"x": "b",
87+
"y": "n",
88+
"z": "m",
89+
"1": "_",
90+
"2": "-",
91+
"3": "|",
92+
"4": "?",
93+
"5": "*",
94+
"6": "!",
95+
"7": "@",
96+
"8": "#",
97+
"9": "$",
98+
"0": "~",
99+
".": "/",
100+
",": "+",
101+
" ": "&",
102+
}
103+
# ==============================
104+
# ----- Notebook With Two Pages -----
105+
self.nb = ttk.Notebook(self.parent)
106+
self.page1 = ttk.Frame(self.nb)
107+
self.page2 = ttk.Frame(self.nb)
108+
self.nb.add(self.page1, text="Encrypt The Words")
109+
self.nb.add(self.page2, text="Decrypt The Words")
110+
self.nb.pack(expand=True, fill="both")
111+
# ----- LabelFrames -----
112+
self.page1_main_label = ttk.LabelFrame(
113+
self.page1, text="Encrypt Any Text"
114+
) # <----- Page1 LabelFrame1
115+
self.page1_main_label.grid(row=0, column=0, pady=20, padx=2, ipadx=20)
116+
self.page1_output_label = ttk.LabelFrame(self.page1, text="Decrypted Text")
117+
self.page1_output_label.grid(row=1, column=0, pady=10, padx=2)
118+
119+
self.page2_main_label = ttk.LabelFrame(
120+
self.page2, text="Decrypt Any Text"
121+
) # <----- Page1 LabelFrame1
122+
self.page2_main_label.grid(row=0, column=0, pady=20, padx=2, ipadx=20)
123+
self.page2_output_label = ttk.LabelFrame(self.page2, text="Real Text")
124+
self.page2_output_label.grid(row=1, column=0, pady=10, padx=2)
125+
# <---Scrolled Text Global
126+
self.decrypted_text_box = ScrolledText(
127+
self.page1_output_label, width=30, height=5, state="normal"
128+
)
129+
self.decrypted_text_box.grid(row=1, column=0, padx=2, pady=10)
130+
131+
self.text_box = ScrolledText(
132+
self.page2_output_label, width=30, height=5, state="normal"
133+
)
134+
self.text_box.grid(row=1, column=0, padx=2, pady=10)
135+
# ----- Variables -----
136+
self.user_text = tk.StringVar()
137+
self.decrypted_user_text = tk.StringVar()
138+
139+
self.user_text2 = tk.StringVar()
140+
self.real_text = tk.StringVar()
141+
# ----- Getting Inside Page1 -----
142+
self.page1_inside()
143+
self.page2_inside()
144+
145+
def page1_inside(self):
146+
style = ttk.Style()
147+
user_text_label = ttk.Label(
148+
self.page1_main_label, text="Enter Your Text Here : ", font=("", 14)
149+
)
150+
user_text_label.grid(row=0, column=0, pady=10)
151+
user_entry_box = ttk.Entry(
152+
self.page1_main_label, width=35, textvariable=self.user_text
153+
)
154+
user_entry_box.grid(row=1, column=0)
155+
style.configure(
156+
"TButton",
157+
foreground="black",
158+
background="white",
159+
relief="groove",
160+
font=("", 12),
161+
)
162+
encrypt_btn = ttk.Button(
163+
self.page1_main_label,
164+
text="Encrypt Text",
165+
style="TButton",
166+
command=self.encrypt_now,
167+
)
168+
encrypt_btn.grid(row=2, column=0, pady=15)
169+
170+
# ---------- Page1 Button Binding Function ----------
171+
172+
def encrypt_now(self):
173+
user_text = self.user_text.get()
174+
if user_text == "":
175+
showerror(
176+
"Nothing Found", "Please Enter Something In Entry Box To Encrypt...!"
177+
)
178+
return
179+
else:
180+
self.decrypted_user_text = self.backend_work("Encrypt", user_text)
181+
self.decrypted_text_box.insert(tk.INSERT, self.decrypted_user_text, tk.END)
182+
183+
# --------------------------------------------------Binding Functions of Page1 End Here
184+
# Page2 ------------------>
185+
def page2_inside(self):
186+
style = ttk.Style()
187+
user_text_label = ttk.Label(
188+
self.page2_main_label, text="Enter Decrypted Text Here : ", font=("", 14)
189+
)
190+
user_text_label.grid(row=0, column=0, pady=10)
191+
user_entry_box = ttk.Entry(
192+
self.page2_main_label, width=35, textvariable=self.user_text2
193+
)
194+
user_entry_box.grid(row=1, column=0)
195+
style.configure(
196+
"TButton",
197+
foreground="black",
198+
background="white",
199+
relief="groove",
200+
font=("", 12),
201+
)
202+
encrypt_btn = ttk.Button(
203+
self.page2_main_label,
204+
text="Decrypt Text",
205+
style="TButton",
206+
command=self.decrypt_now,
207+
)
208+
encrypt_btn.grid(row=2, column=0, pady=15)
209+
# ---------- Page1 Button Binding Function ----------
210+
211+
def decrypt_now(self):
212+
user_text = self.user_text2.get()
213+
if user_text == "":
214+
showerror(
215+
"Nothing Found", "Please Enter Something In Entry Box To Encrypt...!"
216+
)
217+
return
218+
else:
219+
self.real_text = self.backend_work("Decrypt", user_text)
220+
self.text_box.insert(tk.INSERT, self.real_text, tk.END)
221+
222+
def backend_work(self, todo, text_coming):
223+
text_to_return = ""
224+
if todo == "Encrypt":
225+
try:
226+
text_coming = str(
227+
text_coming
228+
) # <----- Lowering the letters as dic in lower letter
229+
for word in text_coming:
230+
for key, value in self.data_dic.items():
231+
if word == key:
232+
# print(word, " : ", key)
233+
text_to_return += value
234+
235+
except ValueError:
236+
showerror("Unknown", "Something Went Wrong, Please Restart Application")
237+
238+
return text_to_return
239+
elif todo == "Decrypt":
240+
try:
241+
text_coming = str(text_coming)
242+
for word in text_coming:
243+
for key, value in self.data_dic.items():
244+
if word == value:
245+
text_to_return += key
246+
247+
except ValueError:
248+
showerror("Unknown", "Something Went Wrong, Please Restart Application")
249+
250+
return text_to_return
251+
252+
else:
253+
showerror("No Function", "Function Could not get what to do...!")
254+
255+
256+
# =============================================================
257+
# ==================== Classes End Here ... ! =================
258+
259+
260+
if __name__ == "__main__":
261+
run = Main()
262+
Notebook(run)
263+
run.mainloop()

0 commit comments

Comments
 (0)