-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
297 lines (202 loc) · 9.14 KB
/
main.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
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
from tkinter import *
import mysql.connector as mysql
from tkinter import messagebox as mb
from tkinter import filedialog
import tkinter.scrolledtext as tks
from csv import DictReader
import _thread
import login
import socket
import json
import account
counter = 1
def main_loop(username):
def add_all_users_to_list():
#db = mysql.connect(host='localhost',user='root',passwd='password',database='login_app')
#for database server hosted online
db = mysql.connect(host='sql12.freemysqlhosting.net',port=3306,user='sql12372553',passwd='XlRVrGbdRf',database='sql12372553')
c = db.cursor()
c.execute(f'select name from log_details')
p = c.fetchall()
for i in p:
all_user_listbox.insert(END,i[0])
def connect_with_server():
global c
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
c.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
#host = 'localhost'
host = '178.22.71.129'
port = 5555
#port = 6666
c.connect((host,port))
msg = {'username':username,'alert':'Online','message':'None'}
c.send(str(msg).encode('utf-8'))
global client
client = c
_thread.start_new_thread(recievingMessage, (c,) )
## Clear the text feild after sending a message
def reset():
msg_entry.delete(0, END)
def recievingMessage (c):
while True :
msg=c.recv(2048).decode('ascii')
j = msg.replace("'","\"")
d = json.loads(j)
print(d)
if d['alert'] == 'Online':
to_write = f'{d["username"]} is Online'
print(to_write)
t = chat_text.get(1.0,END)
chat_text.delete(1.0,END)
chat_text.insert(INSERT,t+to_write+'\n')
chat_text.yview('end')
list_insert(d['user_list'])
elif d['alert'] == 'Offline':
to_write = f'{d["username"]} is Offline'
print(to_write)
t = chat_text.get(1.0,END)
chat_text.delete(1.0,END)
chat_text.insert(INSERT,t+to_write+'\n')
chat_text.yview('end')
list_insert(d['user_list'])
if d['message'] != 'None':
to_write = f'{d["username"]} :- {d["message"]}'
t = chat_text.get(1.0,END)
chat_text.delete(1.0,END)
chat_text.insert(INSERT,t+to_write+'\n')
chat_text.yview('end')
def list_insert(l):
active_user_listbox.delete(0,END)
for i in l:
active_user_listbox.insert(END,i)
def sendMessage (*args):
msg = msg_entry.get()
msg = str({'username':username,'alert':'None','message':msg})
global c
c.send(msg.encode('ascii'))
reset()
def account_func():
account.account_func(username)
def menu_operate_func():
global counter
if counter%2 == 0:
menu_func()
counter += 1
else:
home_func()
counter += 1
def logout_func():
f =open('isLog.txt',"w")
to_write = 'Logged Off,'
f.write(to_write)
f.close()
msg = {'username':username,'alert':'Offline','message':'None'}
c.send(str(msg).encode('utf-8'))
win.destroy()
login.login()
def about_func():
mb.showinfo('About', 'This application was designed to have group chats. This has features like sending messages, showing all users in database, showing all the active users, working in real-time, responsive, managing account settings, login, logout, register. Hope you like the application.')
def user_l_func():
global counter
counter += 1
menu_func()
win.geometry('600x487')
title_label.place(x=500,y=2)
def menu_func():
## Log Bg
log_bg.place(x=1000)
log_img.place(x=1000)
log_in_as.place(x=1000)
name.place(x=1000)
## Menu Options
menu_bg.place(x=1000)
group_b.place(x=1000)
user_l_b.place(x=1000)
setting_b.place(x=1000)
about_b.place(x=1000)
logout_b.place(x=1000)
def hide_list_func():
win.geometry('450x487')
title_label.place(x=350,y=2)
def home_func():
## Log Bg
log_bg.place(x=0,y=33)
log_img.place(x=50,y=50)
log_in_as.place(x=20,y=130)
name.place(x=100,y=131)
## Menu Options
menu_bg.place(x=0,y=32)
group_b.place(x=10,y=200)
user_l_b.place(x=5,y=250)
setting_b.place(x=5,y=300)
about_b.place(x=10,y=360)
logout_b.place(x=10,y=410)
bg = '#213a3b' #41a8ae #213a3b #eaffff
sbg = '#eaffff'
win = Tk()
win.geometry('450x487')
win.configure(bg='#213a3b')
win.resizable(0,0)
win.title('UniChat')
photo = PhotoImage(file = "resources/chat1.png")
win.iconphoto(False, photo)
Label(win, text='',bg=bg,width=450,font=('arial black',15,'bold'),relief='groove',bd=0).pack()
title_label = Label(win, text='UniChat',bg=bg,fg='white',font=('arial black',12,'bold'),bd=0)
title_label.place(x=350,y=2)
menu_img = PhotoImage(file='resources/menu.png')
Button(win, image=menu_img,bg=bg,bd=0,command=menu_operate_func).place(x=8,y=7)
## Group Chat
chat_text = tks.ScrolledText(win, height=19, width=47,font=('Cambria',13),bg=sbg)
chat_text.vbar.config(troughcolor = 'red', bg = 'blue')
chat_text.place(x=10,y=35)
chat_text.yview('end')
msg_entry = Entry(win, font=('Cambria',15),width=31,bg=sbg)
msg_entry.place(x=10,y=430)
send_b = Button(win,fg='white',font=('arial black',10,'bold'), bd=0, text='Send',bg='#41a8ae',width=9,command=sendMessage).place(x=360,y=429)
win.bind('<Return>', sendMessage)
## User List
hide_list = Button(win,fg='white',font=('arial black',10,'bold'),text='Hide User Lists',bg=bg, bd =0, width=15,command=hide_list_func).place(x=450,y=427)
##Menu Bar
menu_bg = Label(win, text='',bg=bg,width=15,font=('arial black',15,'bold'),bd=0, relief='groove',height=16)
log_bg = Label(win,bg=bg,width=30,height=9,relief='groove', bd= 0)
login_img =PhotoImage(file='resources/login.png')
log_img = Label(win,image=login_img,bg=bg)
log_in_as = Label(win, text='Username:',bg=bg,font=('',13),fg='white')
name = Label(win, text=username,bg=bg,font=('',11,'bold'),fg='white')
## if len(username) > 25:
## name['font'] = ('',9,'bold')
mail_img = PhotoImage(file='resources/chat-4-24.png')
group_b = Button(win, image=mail_img, bg=bg,bd=0,text=' Group Chat',compound='left',fg='white',font=('arial black',10,'bold'),command=menu_operate_func)
mul_mail_img = PhotoImage(file='resources/user-4-24.png')
user_l_b = Button(win, image=mul_mail_img, bg=bg,bd=0,text=' User Lists',compound='left',fg='white',font=('arial black',10,'bold'),command=user_l_func)
setting_img = PhotoImage(file='resources/camera-settings-icon-white-300x300.png')
setting_b = Button(win, image=setting_img, bg=bg,bd=0,text=' Account Settings',compound='left',fg='white',font=('arial black',10,'bold'),command=account_func)
about_img = PhotoImage(file='resources/about.png')
about_b = Button(win, image=about_img, bg=bg,bd=0,text=' About',compound='left',fg='white',font=('arial black',10,'bold'),command=about_func)
logout_img = PhotoImage(file='resources/logout-24.png')
logout_b = Button(win, image=logout_img, bg=bg,bd=0,text=' Log Out',compound='left',fg='white',font=('arial black',10,'bold'),command=logout_func)
## User List
Label(win, bg=bg, width=14, text='All Users', fg='White',font=('Cambria',13)).place(x=460,y=50)
all_user_listbox = Listbox(win, width=14, height=7,font=('Cambria',13),bg=sbg, bd=0)
all_user_listbox.place(x=460,y=75)
Label(win, bg=bg, width=14, text='Active Users', fg='White',font=('Cambria',13), bd=0).place(x=460,y=250)
active_user_listbox = Listbox(win, width=14, height=6,font=('Cambria',13),bg=sbg, bd=0)
active_user_listbox.place(x=460,y=275)
connect_with_server()
add_all_users_to_list()
def on_closing():
res = mb.askyesnocancel('Exit','Do you want to logout and Exit?')
if res == True:
f =open('isLog.txt',"w")
to_write = 'Logged Off,'
f.write(to_write)
f.close()
win.destroy()
msg = {'username':username,'alert':'Offline','message':'None'}
c.send(str(msg).encode('utf-8'))
elif res == False:
win.destroy()
msg = {'username':username,'alert':'Offline','message':'None'}
c.send(str(msg).encode('utf-8'))
win.protocol("WM_DELETE_WINDOW", on_closing)
win.mainloop()