-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.py
59 lines (47 loc) · 1.22 KB
/
client.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
import socket
import time
import threading
import tkinter as tk
root = tk.Tk()
HEADER = 64
PORT = 12345
FORMAT = "utf-8"
SERVER = socket.gethostbyname(socket.gethostname())
print(SERVER)
ADDR = (SERVER, PORT)
BUF = 1024
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect(ADDR)
def send(event=None):
message = my_msg.get()
my_msg.set("")
client.send(bytes(message, FORMAT))
if message == "/quit":
time.sleep(1)
client.close()
root.destroy()
def receive():
while True:
try :
msg = client.recv(BUF).decode(FORMAT)
msg_list.insert(tk.END, msg)
except OSError:
break
root.title("GroupChat")
msg_frame = tk.Frame(root)
my_msg = tk.StringVar()
my_msg.set("Type here")
scrollbar = tk.Scrollbar(msg_frame)
msg_list = tk.Listbox(msg_frame, height=20, width=50,yscrollcommand=scrollbar.set)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
msg_list.pack(side=tk.LEFT, fill = tk.BOTH)
msg_list.pack()
msg_frame.pack()
e= tk.Entry(root, textvariable=my_msg)
e.bind("<Return>",send)
e.pack()
send_button=tk.Button(root, text="Send", command=send)
send_button.pack()
thread = threading.Thread(target=receive)
thread.start()
tk.mainloop()