forked from JackZProduction/python_chat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.pyw
111 lines (85 loc) · 3.32 KB
/
client.pyw
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
import thread
from ChatFns import *
#---------------------------------------------------#
#---------INITIALIZE CONNECTION VARIABLES-----------#
#---------------------------------------------------#
WindowTitle = 'JChat v0.1 - Client'
HOST = "YOUR EXTERNAL IP ADDRESS HERE"
PORT = 8011
s = socket(AF_INET, SOCK_STREAM)
#---------------------------------------------------#
#------------------ MOUSE EVENTS -------------------#
#---------------------------------------------------#
def ClickAction():
#Write message to chat window
EntryText = FilteredMessage(EntryBox.get("0.0",END))
LoadMyEntry(ChatLog, EntryText)
#Scroll to the bottom of chat windows
ChatLog.yview(END)
#Erace previous message in Entry Box
EntryBox.delete("0.0",END)
#Send my mesage to all others
s.sendall(EntryText)
#---------------------------------------------------#
#----------------- KEYBOARD EVENTS -----------------#
#---------------------------------------------------#
def PressAction(event):
EntryBox.config(state=NORMAL)
ClickAction()
def DisableEntry(event):
EntryBox.config(state=DISABLED)
#---------------------------------------------------#
#-----------------GRAPHICS MANAGEMENT---------------#
#---------------------------------------------------#
#Create a window
base = Tk()
base.title(WindowTitle)
base.geometry("400x500")
base.resizable(width=FALSE, height=FALSE)
#Create a Chat window
ChatLog = Text(base, bd=0, bg="white", height="8", width="50", font="Arial",)
ChatLog.insert(END, "Connecting to your partner..\n")
ChatLog.config(state=DISABLED)
#Bind a scrollbar to the Chat window
scrollbar = Scrollbar(base, command=ChatLog.yview, cursor="heart")
ChatLog['yscrollcommand'] = scrollbar.set
#Create the Button to send message
SendButton = Button(base, font=30, text="Send", width="12", height=5,
bd=0, bg="#FFBF00", activebackground="#FACC2E",
command=ClickAction)
#Create the box to enter message
EntryBox = Text(base, bd=0, bg="white",width="29", height="5", font="Arial")
EntryBox.bind("<Return>", DisableEntry)
EntryBox.bind("<KeyRelease-Return>", PressAction)
#Place all components on the screen
scrollbar.place(x=376,y=6, height=386)
ChatLog.place(x=6,y=6, height=386, width=370)
EntryBox.place(x=128, y=401, height=90, width=265)
SendButton.place(x=6, y=401, height=90)
#---------------------------------------------------#
#----------------CONNECTION MANAGEMENT--------------#
#---------------------------------------------------#
def ReceiveData():
try:
s.connect((HOST, PORT))
LoadConnectionInfo(ChatLog, '[ Succesfully connected ]\n---------------------------------------------------------------')
except:
LoadConnectionInfo(ChatLog, '[ Unable to connect ]')
return
while 1:
try:
data = s.recv(1024)
except:
LoadConnectionInfo(ChatLog, '\n [ Your partner has disconnected ] \n')
break
if data != '':
LoadOtherEntry(ChatLog, data)
if base.focus_get() == None:
FlashMyWindow(WindowTitle)
playsound('notif.wav')
else:
LoadConnectionInfo(ChatLog, '\n [ Your partner has disconnected ] \n')
break
#s.close()
thread.start_new_thread(ReceiveData,())
base.mainloop()