-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
128 lines (93 loc) · 3.43 KB
/
main.py
File metadata and controls
128 lines (93 loc) · 3.43 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import functools
import sys
import PyQt5.Qt
import socket
class MainWindow(PyQt5.Qt.QWidget):
def __init__(self):
PyQt5.Qt.QWidget.__init__(self)
self.chatbox = PyQt5.Qt.QTextEdit(self)
self.chatbox.setReadOnly(True)
self.chatbox.installEventFilter(self)
self.messagebar = PyQt5.Qt.QLineEdit(self)
self.messagebar.installEventFilter(self)
layout = PyQt5.Qt.QVBoxLayout(self)
layout.addWidget(self.chatbox)
layout.addWidget(self.messagebar)
class UserForm(PyQt5.Qt.QWidget):
def __init__(self):
PyQt5.Qt.QWidget.__init__(self)
parentLayout = PyQt5.Qt.QVBoxLayout()
## QT5 Widgets
self.line = PyQt5.Qt.QLineEdit(self)
self.line.installEventFilter(self)
self.server = PyQt5.Qt.QLineEdit(self)
self.server.installEventFilter(self)
self.port = PyQt5.Qt.QLineEdit(self)
self.port.installEventFilter(self)
## Forum Rows & Layout
forumlayout = PyQt5.Qt.QFormLayout()
forumlayout.addRow("Name:", self.line)
forumlayout.addRow("Server:", self.server)
forumlayout.addRow("Port:", self.port)
## Button Actions and Connectors
button = PyQt5.Qt.QDialogButtonBox()
button.setStandardButtons(
PyQt5.Qt.QDialogButtonBox.Ok | PyQt5.Qt.QDialogButtonBox.Cancel
)
button.accepted.connect(self.close)
button.accepted.connect(self.show_Chat_Window)
button.rejected.connect(sys.exit)
## Add Layout
parentLayout.addLayout(forumlayout)
parentLayout.addWidget(button)
self.setLayout(parentLayout)
## Show Chat Window
def show_Chat_Window(self):
## Network Connection
### Try to Connect
try:
self.networkconnect = ServerConnection()
self.networkconnect.ip = self.server.displayText()
self.networkconnect.port = self.port.displayText()
self.networkconnect.connect(host=self.networkconnect.ip, port=self.networkconnect.port)
## Currently print to console and exit app
except:
print("Could not connect to host")
sys.exit()
ChatWindow.chatbox.wordWrapMode()
ChatWindow.messagebar.returnPressed.connect(self.show_text)
ChatWindow.show()
ChatWindow.activateWindow()
def show_text(self):
HostMessage = ChatWindow.messagebar.displayText()
self.networkconnect.send_message(HostMessage)
ChatWindow.chatbox.append(f"{self.line.displayText()}: {HostMessage}")
ChatWindow.messagebar.clear()
class ServerConnection:
def __init__(self):
self.clientInfo = UserForm()
self.socket = socket.socket()
def connect(self,host,port):
self.host = host
self.port = int(port)
self.server = (self.host, self.port)
self.socket.connect(self.server)
def send_message(self, Message):
self.message = Message
self.socket.send(self.message.encode())
### Global Variables
app = PyQt5.Qt.QApplication(sys.argv)
ChatWindow = MainWindow()
ChatWindow.setGeometry(800, 500, 500, 500)
messages = []
message_history = []
## Functions
def main():
thisUser = UserForm()
thisUser.show()
sys.exit(app.exec())
### Show
# Press the green button in the gutter to run the script.
if __name__ == '__main__':
main()
# See PyCharm help at https://www.jetbrains.com/help/pycharm/