-
Notifications
You must be signed in to change notification settings - Fork 1
/
masterApp.py
153 lines (134 loc) · 7.42 KB
/
masterApp.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
import json
import os
from tkinter import *
from tkinter import messagebox
import datetime
import time
now = datetime.datetime.now()
class Application():
def __init__(self):
self.primaryColor = "#0d1117"
self.secondaryColor = ""
self.title ="Contacts"
self.contactsFile = "contacts.json"
self.countriesFile = "lib/countries.json"
# Interface
self.masterApp = Tk()
#self.masterApp.attributes('-zoomed', True)
self.masterApp.geometry("700x500")
self.masterApp.title("Contact")
self.topbar = Frame(self.masterApp,bg=self.primaryColor)
self.topbar.place(relx=0,rely=0,relwidth=1,relheight=0.1)
self.dateTime = Label(self.topbar, fg="#ffffff",bg=self.primaryColor,text=f"{now.day} / {now.month} / {now.year}", font="Verdana 18 bold")
self.dateTime.pack(side="right")
self.leftbar = Frame(self.masterApp,bg=self.primaryColor)
self.leftbar.place(relx=0,rely=0.15,relwidth=0.35,relheight=0.5)
self.rightbar = Frame(self.masterApp,bg=self.primaryColor)
self.rightbar.place(relx=0.4,rely=0.15,relwidth=0.60,relheight=0.5)
self.footer = Frame(self.masterApp,bg=self.primaryColor)
self.footer.place(relx=0,rely=0.83,relwidth=1,relheight=0.17)
self.title = Label(self.topbar, fg="#ffffff",bg=self.primaryColor,text=self.title, font="Verdana 18 bold")
self.title.pack(side="left")
self.contactNameLabel = Label(self.leftbar, text="Enter Name:", background="#0d1117", fg="white")
self.contactNameLabel.pack(anchor="w")
self.contactNameEntry = Entry(self.leftbar, background="#B2B2B2", fg="black")
self.contactNameEntry.pack(anchor="w")
self.contactSurnameLabel = Label(self.leftbar, text="Enter Surname:", background="#0d1117", fg="white")
self.contactSurnameLabel.pack(anchor="w")
self.contactSurnameEntry = Entry(self.leftbar, background="#B2B2B2", fg="black")
self.contactSurnameEntry.pack(anchor="w")
self.contactPhoneLabel = Label(self.leftbar, text="Enter Phone Number:", background="#0d1117", fg="white")
self.contactPhoneLabel.pack(anchor="w")
self.contactPhoneEntry = Entry(self.leftbar, background="#B2B2B2", fg="black")
self.contactPhoneEntry.pack(anchor="w")
self.contactGroupsLabel = Label(self.leftbar, text="Enter Groups:", background="#0d1117", fg="white")
self.contactGroupsLabel.pack(anchor="w")
self.contactGroupsEntry = Entry(self.leftbar, background="#B2B2B2", fg="black")
self.contactGroupsEntry.pack(anchor="w")
self.setCountryCodeEntry = Entry(self.leftbar, background="#B2B2B2", fg="black")
self.setCountryCodeEntry.pack(anchor="w",side=BOTTOM)
self.setCountryLabel = Label(self.leftbar, text="Set Default Country Code:", background="#0d1117", fg="white")
self.setCountryLabel.pack(anchor="w",side=BOTTOM)
self.addContactButton = Button(self.footer,text="Add Contact",command=self.addNewContact)
self.addContactButton.pack(side=LEFT)
self.setCountryCodeButton = Button(self.footer,text="Set default country code",command=self.setDefaultCountryCode)
self.setCountryCodeButton.pack(side=LEFT)
self.helpButton = Button(self.footer, text="Help", command=self.help)
self.helpButton.pack(side=RIGHT,pady="20")
def addContactGroup(self,groupName,groupDescription):
with open(self.contactsFile, "r") as jsonFile:
data = json.load(jsonFile)
data["contactGroups"].append({"name":groupName,"description":groupDescription})
with open(self.contactsFile, "w") as jsonFile:
json.dump(data, jsonFile, indent=2)
def setDefaultCountryCode(self):
defaultCountryCode = self.setCountryCodeEntry.get()
if defaultCountryCode == '':
messagebox.showerror("Oops!", "Country code can not be empty.")
elif "+" not in defaultCountryCode or len(defaultCountryCode) <= 1:
messagebox.showerror("Oops!", "Country code is invalid.")
else:
with open(self.contactsFile, "r",) as jsonFile:
data = json.load(jsonFile)
data["defaultCountryCode"] = str(defaultCountryCode)
with open(self.contactsFile, "w") as jsonFile:
json.dump(data, jsonFile, indent=2)
messagebox.showinfo("Country code setted",f"Default Country Code setted {defaultCountryCode}.")
print(f"Default Country Code setted {defaultCountryCode}.")
def getCountryCode(self):
with open(self.contactsFile, "r") as jsonFile:
countryCode = json.load(jsonFile)
return countryCode["defaultCountryCode"]
def increaseId(self):
with open(self.contactsFile, "r") as jsonFile:
data = json.load(jsonFile)
data["newContactId"] = data["newContactId"] +1
with open(self.contactsFile, "w") as jsonFile:
json.dump(data, jsonFile, indent=2)
def getId(self):
with open(self.contactsFile, "r") as jsonFile:
data = json.load(jsonFile)
return data["newContactId"]
def addNewContact(self):
contactData = {
"_id": self.getId(),
"name":self.contactNameEntry.get(),
"surname":self.contactSurnameEntry.get(),
"phoneNumber": self.getCountryCode() + self.contactPhoneEntry.get(),
"groups":self.contactGroupsEntry.get().split(sep=",")
}
if(contactData["groups"] == [""]):
contactData["groups"] = []
if(contactData["name"] == ''):
messagebox.showerror("Oops!", "Name can not be empty.")
elif(contactData["surname"] == ''):
messagebox.showerror("Oops!", "Surname can not be empty.")
elif(self.contactPhoneEntry.get() == ''):
messagebox.showerror("Oops!", "Phone Number can not be empty.")
else:
with open(self.contactsFile, "r") as jsonFile:
data = json.load(jsonFile)
data["contactList"].append(contactData)
with open(self.contactsFile, "w") as jsonFile:
json.dump(data, jsonFile, indent=2)
contactBasicInfo=f"{contactData['name'] + ' ' + contactData['surname'].upper()}"
self.increaseId()
messagebox.showinfo("New Contact",f"New Contact {contactBasicInfo} added.")
print(f"New contact {contactBasicInfo} added.")
self.clear_rightbar()
self.getContactList()
def clear_rightbar(self):
for widgets in self.rightbar.winfo_children():
widgets.destroy()
def getContactList(self):
with open(self.contactsFile, "r") as jsonFile:
data = json.load(jsonFile)["contactList"]
scrollbar = Scrollbar(self.rightbar, )
scrollbar.pack( side = RIGHT, fill = Y )
mylist = Listbox(self.rightbar,width=100, height=200, yscrollcommand = scrollbar.set, background="#0d1117", fg="white", font="Verdana 11")
for i in range (1,len(data)):
mylist.insert(END, f"{data[i]['name']} {data[i]['surname']}")
mylist.pack(fill=BOTH)
def help(self):
import webbrowser
webbrowser.open("https://github.com/mertssmnoglu/contacts-app/issues")