-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathhost.py
75 lines (60 loc) · 2.27 KB
/
host.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
from Tkinter import *
from chat import *
from PIL import *
import thread
s = socket(AF_INET, SOCK_STREAM)
HOST = gethostname()
PORT = 9003
conn = ''
s.bind((HOST, PORT))
def onClick():
messageText = messageFilter(textBox.get("0.0",END)) #filter
displayLocalMessage(chatBox, messageText) #display local
chatBox.yview(END) #auto-scroll
textBox.delete("0.0",END) #clear the input box
conn.sendall(messageText) #send over socket
def onEnterButtonPressed(event):
textBox.config(state=NORMAL)
onClick()
def removeKeyboardFocus(event):
textBox.config(state=DISABLED)
def openConnection():
s.listen(2) #Listen for 1 other person
global conn
conn, addr = s.accept()
getConnectionInfo(chatBox, 'Connected with: ' + str(addr) + '\n-------------------------------------')
while 1:
try:
data = conn.recv(1024) #Get data from clients
displayRemoteMessage(chatBox, data) #Display on Remote Windows
except:
getConnectionInfo(chatBox, '\n [ Your partner has disconnected ]\n [ Waiting for him to connect..] \n ')
openConnection()
conn.close()
#Base Window
base = Tk()
base.title("Pychat Host")
base.geometry("400x450")
base.resizable(width=FALSE, height=FALSE)
base.configure(bg="#716664")
#Chat
chatBox = Text(base, bd=0, bg="#689099", height="8", width="20", font="Helvetica",)
chatBox.insert(END, "Waiting for your partner to connect..\n")
chatBox.config(state=DISABLED)
sb = Scrollbar(base, command=chatBox.yview, bg = "#34495e")
chatBox['yscrollcommand'] = sb.set
#Send Button
sendButton = Button(base, font="Helvetica", text="SEND", width="50", height=5,
bd=0, bg="#BDE096", activebackground="#BDE096", justify="center",
command=onClick)
#Text Input
textBox = Text(base, bd=0, bg="#F8B486",width="29", height="5", font="Helvetica")
textBox.bind("<Return>", removeKeyboardFocus)
textBox.bind("<KeyRelease-Return>", onEnterButtonPressed)
#Put everything on the window
sb.place(x=370,y=5, height=350)
chatBox.place(x=15,y=5, height=350, width=355)
sendButton.place(x=255, y=360, height=80, width=130)
textBox.place(x=15, y=360, height=80, width=250)
thread.start_new_thread(openConnection,()) # try listening again upon fail
base.mainloop() #Start the GUI Thread