-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmessenger.py
More file actions
58 lines (46 loc) · 1.61 KB
/
messenger.py
File metadata and controls
58 lines (46 loc) · 1.61 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
"""
перекомпиляция
pyuic5 messenger.ui -o clientui.py
"""
from datetime import datetime
from PyQt5 import QtWidgets, QtCore
import clientui
import requests
class ExampleApp(QtWidgets.QMainWindow, clientui.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.pressed.connect(self.send_message)
self.last_timestamp = 0
self.timer = QtCore.QTimer()
self.timer.timeout.connect(self.update_messages)
self.timer.start(1000)
def send_message(self):
username = self.lineEdit.text()
password = self.lineEdit_2.text()
text = self.textEdit.toPlainText()
requests.get(
"http://127.0.0.1:5000/send_message",
json={
"username": username,
"password": password,
"text": text
}
)
self.textEdit.setText("")
def update_messages(self):
response = requests.get(
"http://127.0.0.1:5000/get_messages",
params={"after": self.last_timestamp}
)
messages = response.json()["messages"]
for message in messages:
dt = datetime.fromtimestamp(message["timestamp"]).strftime("%H:%M:%S %d/%m/%Y")
self.textBrowser.append(dt + " " + message["username"])
self.textBrowser.append(message["text"])
self.textBrowser.append("")
self.last_timestamp = message["timestamp"]
app = QtWidgets.QApplication([])
window = ExampleApp()
window.show()
app.exec_()