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