-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkeyroom.py
More file actions
313 lines (245 loc) · 9.87 KB
/
keyroom.py
File metadata and controls
313 lines (245 loc) · 9.87 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import tkinter.messagebox
import sqlite3
from tkinter import *
from PIL import Image, ImageTk
from os.path import exists
# Variables for common use.
bg_colour = "#425587"
bg2_colour = "#384873"
bg3_colour = "#2f3c61"
url, lgn, pwd, eml, typ = '', '', '', '', ''
status_msg = 'Writed by PJ... '
database_name = 'keyroom.db'
file_exists = exists(database_name)
# Database object. Connection, close and create table.
class Database:
# Make connection/create new DB.
def __init__(self, database_name):
self.connection = sqlite3.connect(database_name)
self.cursor = self.connection.cursor()
# Close connection.
def __del__(self):
self.connection.close()
# Create new table.
def create_table(self, sql: str):
self.cursor.execute(sql)
self.connection.commit()
def new_table():
"""Creating database and table with information for user"""
global status_msg
popup = tkinter.messagebox.showinfo('INFO!!!', 'No database, making new...')
db = Database(database_name)
# Creating new table in DB.
db.create_table('''CREATE TABLE passes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
url TEXT,
lgn TEXT,
pwd TEXT,
eml TEXT,
typ TEXT)''')
# Popup message with info and instruction
popup2 = tkinter.messagebox.showinfo('INFO!!!', 'Created new database...')
popup3 = tkinter.messagebox.showinfo('HOW TO USE:',
'1. To add a record to the database, enter the data and press WRITE.\n '
'2. To read a record, enter URL or SHORT and hit READ.\n '
'3. To delete a record, enter URL or SHORT and press DELETE.\n'
'4. To update a record, read from DB, correct the data and press UPDATE.\n'
'5. The CLEAR button will clear all fields in form.\n '
'6. Enter "SHOW" in url to see all SHORTs in DB ')
def read():
"""Read records from database and display in entry boxes"""
global status_msg
global url, lgn, pwd, eml, typ
status_msg = 'Reading record from database... '
url = url_entry.get()
typ = typ_entry.get()
db = Database(database_name)
# Check if entered data is valic.
if url != '' and typ == '':
# If entered 'SHOW' popup message with SHORTS.
if url.lower() == 'show':
sql = "SELECT typ FROM passes"
db.cursor.execute(sql)
record = db.cursor.fetchall()
popup4 = tkinter.messagebox.showinfo('Show shorts in DB:', ' '.join(map(str, record)))
sql = "SELECT * FROM passes where url = ?"
db.cursor.execute(sql, (url, ))
record = db.cursor.fetchall()
if not record:
status_msg = 'No records under this URL... '
for data in record:
lgn_entry.insert(0, data[2])
pwd_entry.insert(0, data[3])
eml_entry.insert(0, data[4])
typ_entry.insert(0, data[5])
elif url == '' and typ != '':
sql = "SELECT * FROM passes where typ = ?"
db.cursor.execute(sql, (typ,))
record = db.cursor.fetchall()
if not record:
status_msg = 'No records under this SHORT... '
for data in record:
url_entry.insert(0, data[1])
lgn_entry.insert(0, data[2])
pwd_entry.insert(0, data[3])
eml_entry.insert(0, data[4])
elif url != '' and typ != '':
status_msg = 'Enter URL or SHORT to read records... '
else:
status_msg = 'Nothing in entry URL or SHORT... '
db.connection.close()
status_update()
def clear():
"""Wiping all entry boxes"""
global status_msg, url_entry, lgn_entry, pwd_entry, eml_entry, typ_entry
url_entry.delete(0, "end")
lgn_entry.delete(0, "end")
pwd_entry.delete(0, "end")
eml_entry.delete(0, "end")
typ_entry.delete(0, "end")
status_msg = 'Wipe entry boxes... '
status_update()
def update():
"""Update data record in database"""
global status_msg
global url, lgn, pwd, eml, typ
url = url_entry.get().strip()
lgn = lgn_entry.get().strip()
pwd = pwd_entry.get().strip()
eml = eml_entry.get().strip()
typ = typ_entry.get().strip()
sql = "UPDATE passes SET url = ?, lgn = ?, pwd = ?, eml = ? WHERE typ = ?"
db = Database(database_name)
# Check SHORT value:
if typ != '':
db.cursor.execute(sql, (url, lgn, pwd, eml, typ))
db.connection.commit()
status_msg = 'Updating record in database... '
else:
status_msg = 'To update record need SHORT... '
db.connection.close()
status_update()
def write():
"""Creating new record from form, inserting in table."""
global status_msg
global url, lgn, pwd, eml, typ
sql = "INSERT INTO passes(url, lgn, pwd, eml, typ) VALUES (?,?,?,?,?)"
data = (url_entry.get(), lgn_entry.get(), pwd_entry.get(), eml_entry.get(), typ_entry.get(),)
# Check if form is not empty:
if url_entry.get().strip() and lgn_entry.get().strip() and typ_entry.get().strip() != '':
db = Database(database_name)
db.cursor.execute(sql, data)
db.connection.commit()
db.connection.close()
status_msg = 'Write record to database... '
status_update()
else:
status_msg = 'No data to write in database... '
status_update()
def delete():
"""Delete record from database"""
global status_msg
global url, lgn, pwd, eml, typ
url = url_entry.get().strip()
typ = typ_entry.get().strip()
db = Database(database_name)
# Check if URL or SHORT is entered:
if url != '':
sql = "DELETE FROM passes WHERE url = ?"
db.cursor.execute(sql, (url,))
status_msg = 'Deleting record in database... '
elif typ != '':
sql = "DELETE FROM passes WHERE typ = ?"
db.cursor.execute(sql, (typ,))
status_msg = 'Deleting record in database... '
else:
status_msg = 'Need URL or SHORT to delete record... '
db.connection.commit()
db.connection.close()
status_update()
clear()
def status_update():
"""Update information in status bar"""
status_lbl = Label(frame1, text=status_msg, bd=1, fg='white', bg=bg_colour, relief=GROOVE, anchor='e')
status_lbl.grid(row=5, column=0, columnspan=4, pady=15, sticky='we')
# Init GUI.
root = Tk()
root.title('KeyRoom')
root.resizable(width=False, height=False)
root.iconbitmap('assets/lock_icon.ico')
# Load and resize logo1 and convert to TkImage.
logo1 = Image.open('assets/lock.png')
logo1 = logo1.resize((180, 180))
logo1 = ImageTk.PhotoImage(logo1)
# Create app body from 'frame'.
frame1 = Frame(root, width=720, height=220, bg=bg_colour)
frame1.grid(row=0, column=0, columnspan=4, sticky='nesw')
frame1.grid_propagate(False)
# Create image widget (Logo).
logo1_lbl = Label(frame1, image=logo1, bg=bg_colour)
logo1_lbl.grid(row=0, rowspan=5, column=0)
# Creating labels and entry in frame1 grid:
# URL section.
url_msg = StringVar()
url_msg.set(url)
url_lbl = Label(frame1, text='URL: ', bg=bg_colour, fg='white', font=('bold', 14))
url_lbl.grid(row=0, column=1,)
url_entry = Entry(frame1, textvariable=url_msg, fg='white', bg=bg_colour, bd=1, width=30, font=12)
url_entry.grid(row=0, column=2)
# LOGIN section.
lgn_msg = StringVar()
lgn_msg.set(lgn)
lgn_lbl = Label(frame1, text='LOGIN: ', bg=bg_colour, fg='white', font=('bold', 14))
lgn_lbl.grid(row=1, column=1)
lgn_entry = Entry(frame1, textvariable=lgn_msg, fg='white', bg=bg_colour, bd=1, width=30, font=12)
lgn_entry.grid(row=1, column=2)
# PASS section.
pwd_msg = StringVar()
pwd_msg.set(pwd)
pwd_lbl = Label(frame1, text='PASS: ', bg=bg_colour, fg='white', font=('bold', 14))
pwd_lbl.grid(row=2, column=1)
pwd_entry = Entry(frame1, textvariable=pwd_msg, fg='white', bg=bg_colour, bd=1, width=30, font=12)
pwd_entry.grid(row=2, column=2)
# EMAIL section.
eml_msg = StringVar()
eml_msg.set(eml)
eml_lbl = Label(frame1, text='EMAIL: ', bg=bg_colour, fg='white', font=('bold', 14))
eml_lbl.grid(row=3, column=1)
eml_entry = Entry(frame1, textvariable=eml_msg, fg='white', bg=bg_colour, bd=1, width=30, font=12)
eml_entry.grid(row=3, column=2)
# Type section (Only for making fifth row =)).
typ_msg = StringVar()
typ_msg.set(typ)
typ_lbl = Label(frame1, text='SHORT: ', bg=bg_colour, fg='white', font=('bold', 14))
typ_lbl.grid(row=4, column=1)
typ_entry = Entry(frame1, textvariable=typ_msg, fg='white', bg=bg_colour, bd=1, width=30, font=12)
typ_entry.grid(row=4, column=2)
# Creating buttons in frame1 grin:
# Read button.
read_bt = Button(frame1, text='Read', font=14, width=15, fg='white', bg=bg2_colour,
activebackground=bg3_colour, command=lambda: read())
read_bt.grid(row=0, column=3, padx=20)
# Clear button.
clear_bt = Button(frame1, text='Clear', font=14, width=15, fg='white', bg=bg2_colour,
activebackground=bg3_colour, command=lambda: clear())
clear_bt.grid(row=1, column=3, padx=20)
# Write button.
write_bt = Button(frame1, text='Write', font=14, width=15, fg='white', bg=bg2_colour,
activebackground=bg3_colour, command=lambda: write())
write_bt.grid(row=2, column=3, padx=20)
# Update button.
update_bt = Button(frame1, text='Update', font=14, width=15, fg='white', bg=bg2_colour,
activebackground=bg3_colour, command=lambda: update())
update_bt.grid(row=3, column=3, padx=20)
# Delete button.
delete_bt = Button(frame1, text='Delete', font=14, width=15, fg='white', bg=bg2_colour,
activebackground=bg3_colour, command=lambda: delete())
delete_bt.grid(row=4, column=3, padx=20)
# Creating status bar.
status_update()
# Check if DB existing.
if not file_exists:
new_table()
# Only for console test.
print('App created by PJ... ')
root.mainloop()