-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
77 lines (69 loc) · 2.62 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import sys,time
from PyQt5 import QtGui
from PyQt5 import QtCore
from PyQt5.QtWidgets import QScrollBar,QSplitter,QTableWidgetItem,QTableWidget,QComboBox,QVBoxLayout,QGridLayout,QDialog,QWidget, QPushButton, QApplication, QMainWindow,QAction,QMessageBox,QLabel,QTextEdit,QProgressBar,QLineEdit
from PyQt5.QtCore import QCoreApplication
import socket
from threading import Thread
from socketserver import ThreadingMixIn
tcpClientA=None
class Window(QDialog):
def __init__(self):
super().__init__()
self.flag=0
self.chatTextField=QLineEdit(self)
self.chatTextField.resize(480,100)
self.chatTextField.move(10,350)
self.btnSend=QPushButton("send",self)
self.btnSend.resize(480,30)
self.btnSendFont=self.btnSend.font()
self.btnSendFont.setPointSize(15)
self.btnSend.setFont(self.btnSendFont)
self.btnSend.move(10,460)
self.btnSend.setStyleSheet("background-color: #0e7ad4 ")
self.btnSend.clicked.connect(self.send)
self.chatBody=QVBoxLayout(self)
splitter=QSplitter(QtCore.Qt.Vertical)
self.chat = QTextEdit()
self.chat.setReadOnly(True)
splitter.addWidget(self.chat)
splitter.addWidget(self.chatTextField)
splitter.setSizes([400,100])
splitter2=QSplitter(QtCore.Qt.Vertical)
splitter2.addWidget(splitter)
splitter2.addWidget(self.btnSend)
splitter2.setSizes([200,10])
self.chatBody.addWidget(splitter2)
self.setWindowTitle("Chat Application")
self.resize(500, 500)
def send(self):
text=self.chatTextField.text()
font=self.chat.font()
font.setPointSize(13)
self.chat.setFont(font)
textFormatted='{:>80}'.format(text)
self.chat.append(textFormatted)
tcpClientA.send(text.encode())
self.chatTextField.setText("")
class ClientThread(Thread):
def __init__(self,window):
Thread.__init__(self)
self.window=window
def run(self):
host = socket.gethostname()
port = 8080
BUFFER_SIZE = 2000
global tcpClientA
tcpClientA = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpClientA.connect((host, port))
while True:
data = tcpClientA.recv(BUFFER_SIZE)
window.chat.append(data.decode("utf-8"))
tcpClientA.close()
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
clientThread=ClientThread(window)
clientThread.start()
window.exec()
sys.exit(app.exec_())