Issue
This Content is from Stack Overflow. Question asked by Nervozs
I’ve only just started using pyQt6 as of today, as tkinter was giving me some issues, however now that I have the base of the app working, I’m faced with an issue, my chat (self.output) will only update with new messages anytime that the send button is pressed, this of course is not how I would like it to be working, I’d rather it update once a message has been sent from the server, is there any solution to this problem?
Here is the code
from ast import Delete
import socket
import threading
from PyQt6.QtWidgets import *
from PyQt6.QtGui import *
from PyQt6.QtCore import *
import rsa
import sys
app = QApplication(sys.argv)
HOST = '127.0.0.1'
PORT = 9090
shost = '127.0.0.1'
sport = 9090
def loadKeys():
with open('keys/publicKey.pem', 'rb') as p:
publicKey = rsa.PublicKey.load_pkcs1(p.read())
with open('keys/privateKey.pem', 'rb') as p:
privateKey = rsa.PrivateKey.load_pkcs1(p.read())
return privateKey, publicKey
def encrypt(message, key):
return rsa.encrypt(message.encode('utf-8'), key)
def decrypt(ciphertext, key):
try:
return rsa.decrypt(ciphertext, key).decode('utf-8')
except:
return False
def sign(message, key):
return rsa.sign(message.encode('utf-8'), key, 'SHA-1')
def verify(message, signature, key):
try:
return rsa.verify(message.encode('utf-8'), signature, key,) == 'SHA-1'
except:
return False
privateKey, publicKey = loadKeys()
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((shost,sport))
gui_done = False
running = True
class Chatroom(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Harmonic")
self.resize(1920,1080)
self.nickname = QInputDialog.getText(self, 'Nickname', 'Enter your nickname:')
layout = QVBoxLayout()
self.setLayout(layout)
QApplication.processEvents()
self.inputField = QLineEdit()
button = QPushButton('&Send')
button.clicked.connect(self.write)
self.output = QTextEdit()
layout.addWidget(self.inputField)
layout.addWidget(button)
layout.addWidget(self.output)
self.Gui_done = True
self.restoremessages()
receive_thread = threading.Thread(target=self.receive)
receive_thread.start()
def restoremessages(self):
with open('chathistory.txt', 'r') as file:
lines = file.readlines()
for line in lines:
self.output.append('{0}'.format(line))
def write(self):
message = (f"{self.nickname}: {self.inputField.text()}n")
emessage = encrypt(message, publicKey)
sock.send(emessage)
self.inputField.clear()
def stop(self):
running = False
sock.close
sys.exit(app.exec_())
def receive(self):
while running:
try:
message = sock.recv(10000)
dm = decrypt(message,privateKey)
if message == "NICK":
sock.send(self.nickname.encode())
else:
self.output.append('{0}'.format(dm))
QApplication.processEvents
except ConnectionAbortedError:
print("Connection aborted")
sock.close()
break
except:
print("Error")
sock.close()
break
def closeEvent(self,event):
close = QMessageBox.question(self, "QUIT", "Are you sure you want to stop the process?", QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
if close == QMessageBox.StandardButton.Yes:
event.accept()
self.stop()
else:
event.ignore()
window = Chatroom()
window.show()
app.exec()
There are certain parts of the code that are redundent that I am aware of, same the error with sys.exit(app.exec_())
for some reason the program would not exit without me throwing an error to do so, so this will have to do for now.
Solution
This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.
This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.