-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchat.py
More file actions
79 lines (61 loc) · 1.8 KB
/
chat.py
File metadata and controls
79 lines (61 loc) · 1.8 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
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
import platform
import socket
import select
import threading
from PySide6.QtWidgets import (
QApplication
,QWidget
,QLabel
,QPushButton
,QVBoxLayout
,QPlainTextEdit
,QLineEdit
)
connected = False
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
app = QApplication([])
app.setStyle(platform.system().lower())
window = QWidget()
v_layout = QVBoxLayout()
btn1 = QPushButton('Send')
btn1.setShortcut("return")
chatInput = QLineEdit("localhost:PORT")
chatInput.setMaximumSize(window.width(), 50)
chatb = QPlainTextEdit("---This is the start of the chat---")
chatb.setReadOnly(True)
chatb.setMinimumSize(window.width(), window.height())
v_layout.addWidget(QLabel('CHATBOX!'))
v_layout.addWidget(chatb)
v_layout.addWidget(chatInput)
v_layout.addWidget(btn1)
def on_btn1_clicked():
global connected, server
if not connected:
connected = True
text = chatInput.text()
chatInput.setText("")
text = text.replace(" ","",text.count(" "))
text = text.split(":")
ip = text[0]
port = int(text[1])
server.connect((ip,port))
def whileLoop():
while True:
sockets_list = [server]
read_sockets, write_socket, error_socket = select.select(sockets_list, [], [])
for socks in read_sockets:
receive_message(str(socks.recv(2048), "utf-8"))
server.close()
t1 = threading.Thread(target=whileLoop)
t1.start()
else:
message = chatInput.text()
server.send(bytes(message, "utf-8"))
chatb.appendPlainText("<YOU> " + message)
chatInput.setText("")
def receive_message(message):
chatb.appendPlainText(message)
btn1.clicked.connect(on_btn1_clicked)
window.setLayout(v_layout)
window.show()
app.exec()