-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient1.py
More file actions
45 lines (36 loc) · 1.02 KB
/
Copy pathclient1.py
File metadata and controls
45 lines (36 loc) · 1.02 KB
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
from socket import *
import threading
from tkinter import *
address='127.0.0.1' #服务器的ip地址
port=9000
buffsize=1024
s=socket(AF_INET, SOCK_STREAM)
s.connect((address,port))
def recv():
while True:
recvdata = s.recv(buffsize).decode('utf-8')
gui.listBox.insert(END, recvdata)
print('\n' + recvdata + '\n')
class GUI:
def __init__(self, root):
self.root = root
self.listBox = Listbox(self.root)
self.listBox.pack()
self.entry = Entry(self.root)
self.entry.pack()
self.sendBtn = Button(self.root, text='发送', command=self.send)
self.sendBtn.pack()
def send(self):
senddata = self.entry.get()
s.send(senddata.encode())
def createGUI():
global gui
root = Tk()
gui = GUI(root)
root.title('客户端')
root.mainloop()
if __name__ == '__main__':
t1 = threading.Thread(target=recv, args=(), name='recv')
t2 = threading.Thread(target=createGUI, args=(), name='gui')
t1.start()
t2.start()