PyQt --> addItem --> TypeError: arguments did not match any overloaded call:
up vote
1
down vote
favorite
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'chatbox.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
import sys, socket
from threading import *
import _thread
def app_version():
msg_box("Application Version", "Test 0.1")
def msg_box(title, data):
w = QWidget()
QMessageBox.information(w, title, data)
def update_list(self, data):
self.listWidget.addItem(data)
print ("a")
def server_socket(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 10420))
s.listen(1)
except socket.error:
msg_box("Socket Error !!",
"Unable To Setup Local Socket. Port In Use")
return
while 1:
conn, addr = s.accept()
incoming_ip = str(addr[0])
current_chat_ip = self.lineEdit.text()
if incoming_ip != current_chat_ip:
conn.close()
else:
data = conn.recv(4096)
update_list(self, data)
conn.close()
s.close()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.start_server()
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 646)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(80, 50, 671, 31))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(70, 0, 331, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 0, 61, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(440, 0, 55, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 0, 171, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(80, 90, 401, 411))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.textEdit = QtWidgets.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.textEdit.setObjectName("textEdit")
self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(20, 380, 151, 28))
self.pushButton_3.setObjectName("pushButton_3")
#############################################################
# Executes When The Send Message Button Is Clicked
self.pushButton_3.clicked.connect(self.client_send_message)
############################################################
self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(230, 380, 151, 28))
self.pushButton_4.setObjectName("pushButton_4")
#############################################################
# Executes When The Clear Logs Button Is Clicked
self.pushButton_4.clicked.connect(self.clear_logs)
##############################################################
self.frame_3 = QtWidgets.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(490, 80, 261, 421))
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.listWidget = QtWidgets.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 241, 401))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 860, 26))
self.menubar.setObjectName("menubar")
self.menuMenu_Action = QtWidgets.QMenu(self.menubar)
self.menuMenu_Action.setObjectName("menuMenu_Action")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionVersion = QtWidgets.QAction(MainWindow)
self.actionVersion.setObjectName("actionVersion")
#######################################################
# Executes When The SubMenu Item Version Is Clicked
self.actionVersion.triggered.connect(app_version)
#######################################################
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
#######################################################
# Executes When The SubMenu Item Exit Is Clicked
self.actionExit.triggered.connect(qApp.quit)
#######################################################
self.menuMenu_Action.addAction(self.actionVersion)
self.menuMenu_Action.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Action.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def clear_logs(self):
self.listWidget.clear()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "IP address"))
self.label_2.setText(_translate("MainWindow", "Name"))
self.pushButton_3.setText(_translate("MainWindow", "Send"))
self.pushButton_4.setText(_translate("MainWindow", "Clear logs"))
self.menuMenu_Action.setTitle(_translate("MainWindow", "Menu Action"))
self.actionVersion.setText(_translate("MainWindow", "Version"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
def start_server(self):
Thread(target=server_socket, args=(self,)).start()
msg_box("Success", "Server Started Sucessfully")
def client_send_message(self):
ip_address = self.lineEdit.text()
nick = self.lineEdit_2.text()
nick = nick.replace("#>","")
rmessage = self.textEdit.toPlainText()
rmessage = rmessage.replace("#>","")
rmsg = nick + " #> " + rmessage
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
c.connect((ip_address, 10420))
except Exception:
msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
return
try:
c.send(rmsg)
self.listWidget.addItem(rmsg)
self.textEdit.setText("")
except Exception:
msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
c.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I try to make chat app base on https://null-byte.wonderhowto.com/how-to/sploit-build-peer-peer-chat-application-python-gui-linux-0163617/
And get error
Exception in thread Thread-1: Traceback (most recent call last):
File "D:Program Filespython 3.7libthreading.py", line 917, in_bootstrap_inner
self.run()
File "D:Program Filespython 3.7libthreading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "chatbox.py", line 47, in server_socket
update_list(self, data)
File "chatbox.py", line 24, in update_list
self.listWidget.addItem(data)
TypeError: arguments did not match any overloaded call:
addItem(self, QListWidgetItem): argument 1 has unexpected type 'bytes'
addItem(self, str): argument 1 has unexpected type 'bytes'
I just begin code, so please forgive if having mistake
python pyqt pyqt5
add a comment |
up vote
1
down vote
favorite
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'chatbox.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
import sys, socket
from threading import *
import _thread
def app_version():
msg_box("Application Version", "Test 0.1")
def msg_box(title, data):
w = QWidget()
QMessageBox.information(w, title, data)
def update_list(self, data):
self.listWidget.addItem(data)
print ("a")
def server_socket(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 10420))
s.listen(1)
except socket.error:
msg_box("Socket Error !!",
"Unable To Setup Local Socket. Port In Use")
return
while 1:
conn, addr = s.accept()
incoming_ip = str(addr[0])
current_chat_ip = self.lineEdit.text()
if incoming_ip != current_chat_ip:
conn.close()
else:
data = conn.recv(4096)
update_list(self, data)
conn.close()
s.close()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.start_server()
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 646)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(80, 50, 671, 31))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(70, 0, 331, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 0, 61, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(440, 0, 55, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 0, 171, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(80, 90, 401, 411))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.textEdit = QtWidgets.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.textEdit.setObjectName("textEdit")
self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(20, 380, 151, 28))
self.pushButton_3.setObjectName("pushButton_3")
#############################################################
# Executes When The Send Message Button Is Clicked
self.pushButton_3.clicked.connect(self.client_send_message)
############################################################
self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(230, 380, 151, 28))
self.pushButton_4.setObjectName("pushButton_4")
#############################################################
# Executes When The Clear Logs Button Is Clicked
self.pushButton_4.clicked.connect(self.clear_logs)
##############################################################
self.frame_3 = QtWidgets.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(490, 80, 261, 421))
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.listWidget = QtWidgets.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 241, 401))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 860, 26))
self.menubar.setObjectName("menubar")
self.menuMenu_Action = QtWidgets.QMenu(self.menubar)
self.menuMenu_Action.setObjectName("menuMenu_Action")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionVersion = QtWidgets.QAction(MainWindow)
self.actionVersion.setObjectName("actionVersion")
#######################################################
# Executes When The SubMenu Item Version Is Clicked
self.actionVersion.triggered.connect(app_version)
#######################################################
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
#######################################################
# Executes When The SubMenu Item Exit Is Clicked
self.actionExit.triggered.connect(qApp.quit)
#######################################################
self.menuMenu_Action.addAction(self.actionVersion)
self.menuMenu_Action.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Action.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def clear_logs(self):
self.listWidget.clear()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "IP address"))
self.label_2.setText(_translate("MainWindow", "Name"))
self.pushButton_3.setText(_translate("MainWindow", "Send"))
self.pushButton_4.setText(_translate("MainWindow", "Clear logs"))
self.menuMenu_Action.setTitle(_translate("MainWindow", "Menu Action"))
self.actionVersion.setText(_translate("MainWindow", "Version"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
def start_server(self):
Thread(target=server_socket, args=(self,)).start()
msg_box("Success", "Server Started Sucessfully")
def client_send_message(self):
ip_address = self.lineEdit.text()
nick = self.lineEdit_2.text()
nick = nick.replace("#>","")
rmessage = self.textEdit.toPlainText()
rmessage = rmessage.replace("#>","")
rmsg = nick + " #> " + rmessage
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
c.connect((ip_address, 10420))
except Exception:
msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
return
try:
c.send(rmsg)
self.listWidget.addItem(rmsg)
self.textEdit.setText("")
except Exception:
msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
c.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I try to make chat app base on https://null-byte.wonderhowto.com/how-to/sploit-build-peer-peer-chat-application-python-gui-linux-0163617/
And get error
Exception in thread Thread-1: Traceback (most recent call last):
File "D:Program Filespython 3.7libthreading.py", line 917, in_bootstrap_inner
self.run()
File "D:Program Filespython 3.7libthreading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "chatbox.py", line 47, in server_socket
update_list(self, data)
File "chatbox.py", line 24, in update_list
self.listWidget.addItem(data)
TypeError: arguments did not match any overloaded call:
addItem(self, QListWidgetItem): argument 1 has unexpected type 'bytes'
addItem(self, str): argument 1 has unexpected type 'bytes'
I just begin code, so please forgive if having mistake
python pyqt pyqt5
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'chatbox.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
import sys, socket
from threading import *
import _thread
def app_version():
msg_box("Application Version", "Test 0.1")
def msg_box(title, data):
w = QWidget()
QMessageBox.information(w, title, data)
def update_list(self, data):
self.listWidget.addItem(data)
print ("a")
def server_socket(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 10420))
s.listen(1)
except socket.error:
msg_box("Socket Error !!",
"Unable To Setup Local Socket. Port In Use")
return
while 1:
conn, addr = s.accept()
incoming_ip = str(addr[0])
current_chat_ip = self.lineEdit.text()
if incoming_ip != current_chat_ip:
conn.close()
else:
data = conn.recv(4096)
update_list(self, data)
conn.close()
s.close()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.start_server()
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 646)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(80, 50, 671, 31))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(70, 0, 331, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 0, 61, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(440, 0, 55, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 0, 171, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(80, 90, 401, 411))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.textEdit = QtWidgets.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.textEdit.setObjectName("textEdit")
self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(20, 380, 151, 28))
self.pushButton_3.setObjectName("pushButton_3")
#############################################################
# Executes When The Send Message Button Is Clicked
self.pushButton_3.clicked.connect(self.client_send_message)
############################################################
self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(230, 380, 151, 28))
self.pushButton_4.setObjectName("pushButton_4")
#############################################################
# Executes When The Clear Logs Button Is Clicked
self.pushButton_4.clicked.connect(self.clear_logs)
##############################################################
self.frame_3 = QtWidgets.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(490, 80, 261, 421))
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.listWidget = QtWidgets.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 241, 401))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 860, 26))
self.menubar.setObjectName("menubar")
self.menuMenu_Action = QtWidgets.QMenu(self.menubar)
self.menuMenu_Action.setObjectName("menuMenu_Action")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionVersion = QtWidgets.QAction(MainWindow)
self.actionVersion.setObjectName("actionVersion")
#######################################################
# Executes When The SubMenu Item Version Is Clicked
self.actionVersion.triggered.connect(app_version)
#######################################################
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
#######################################################
# Executes When The SubMenu Item Exit Is Clicked
self.actionExit.triggered.connect(qApp.quit)
#######################################################
self.menuMenu_Action.addAction(self.actionVersion)
self.menuMenu_Action.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Action.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def clear_logs(self):
self.listWidget.clear()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "IP address"))
self.label_2.setText(_translate("MainWindow", "Name"))
self.pushButton_3.setText(_translate("MainWindow", "Send"))
self.pushButton_4.setText(_translate("MainWindow", "Clear logs"))
self.menuMenu_Action.setTitle(_translate("MainWindow", "Menu Action"))
self.actionVersion.setText(_translate("MainWindow", "Version"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
def start_server(self):
Thread(target=server_socket, args=(self,)).start()
msg_box("Success", "Server Started Sucessfully")
def client_send_message(self):
ip_address = self.lineEdit.text()
nick = self.lineEdit_2.text()
nick = nick.replace("#>","")
rmessage = self.textEdit.toPlainText()
rmessage = rmessage.replace("#>","")
rmsg = nick + " #> " + rmessage
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
c.connect((ip_address, 10420))
except Exception:
msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
return
try:
c.send(rmsg)
self.listWidget.addItem(rmsg)
self.textEdit.setText("")
except Exception:
msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
c.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I try to make chat app base on https://null-byte.wonderhowto.com/how-to/sploit-build-peer-peer-chat-application-python-gui-linux-0163617/
And get error
Exception in thread Thread-1: Traceback (most recent call last):
File "D:Program Filespython 3.7libthreading.py", line 917, in_bootstrap_inner
self.run()
File "D:Program Filespython 3.7libthreading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "chatbox.py", line 47, in server_socket
update_list(self, data)
File "chatbox.py", line 24, in update_list
self.listWidget.addItem(data)
TypeError: arguments did not match any overloaded call:
addItem(self, QListWidgetItem): argument 1 has unexpected type 'bytes'
addItem(self, str): argument 1 has unexpected type 'bytes'
I just begin code, so please forgive if having mistake
python pyqt pyqt5
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'chatbox.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore,QtGui,QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import*
import sys, socket
from threading import *
import _thread
def app_version():
msg_box("Application Version", "Test 0.1")
def msg_box(title, data):
w = QWidget()
QMessageBox.information(w, title, data)
def update_list(self, data):
self.listWidget.addItem(data)
print ("a")
def server_socket(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 10420))
s.listen(1)
except socket.error:
msg_box("Socket Error !!",
"Unable To Setup Local Socket. Port In Use")
return
while 1:
conn, addr = s.accept()
incoming_ip = str(addr[0])
current_chat_ip = self.lineEdit.text()
if incoming_ip != current_chat_ip:
conn.close()
else:
data = conn.recv(4096)
update_list(self, data)
conn.close()
s.close()
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.start_server()
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 646)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(80, 50, 671, 31))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(70, 0, 331, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 0, 61, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(440, 0, 55, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 0, 171, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(80, 90, 401, 411))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.textEdit = QtWidgets.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.textEdit.setObjectName("textEdit")
self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(20, 380, 151, 28))
self.pushButton_3.setObjectName("pushButton_3")
#############################################################
# Executes When The Send Message Button Is Clicked
self.pushButton_3.clicked.connect(self.client_send_message)
############################################################
self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(230, 380, 151, 28))
self.pushButton_4.setObjectName("pushButton_4")
#############################################################
# Executes When The Clear Logs Button Is Clicked
self.pushButton_4.clicked.connect(self.clear_logs)
##############################################################
self.frame_3 = QtWidgets.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(490, 80, 261, 421))
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.listWidget = QtWidgets.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 241, 401))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 860, 26))
self.menubar.setObjectName("menubar")
self.menuMenu_Action = QtWidgets.QMenu(self.menubar)
self.menuMenu_Action.setObjectName("menuMenu_Action")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionVersion = QtWidgets.QAction(MainWindow)
self.actionVersion.setObjectName("actionVersion")
#######################################################
# Executes When The SubMenu Item Version Is Clicked
self.actionVersion.triggered.connect(app_version)
#######################################################
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
#######################################################
# Executes When The SubMenu Item Exit Is Clicked
self.actionExit.triggered.connect(qApp.quit)
#######################################################
self.menuMenu_Action.addAction(self.actionVersion)
self.menuMenu_Action.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Action.menuAction())
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def clear_logs(self):
self.listWidget.clear()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "IP address"))
self.label_2.setText(_translate("MainWindow", "Name"))
self.pushButton_3.setText(_translate("MainWindow", "Send"))
self.pushButton_4.setText(_translate("MainWindow", "Clear logs"))
self.menuMenu_Action.setTitle(_translate("MainWindow", "Menu Action"))
self.actionVersion.setText(_translate("MainWindow", "Version"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
def start_server(self):
Thread(target=server_socket, args=(self,)).start()
msg_box("Success", "Server Started Sucessfully")
def client_send_message(self):
ip_address = self.lineEdit.text()
nick = self.lineEdit_2.text()
nick = nick.replace("#>","")
rmessage = self.textEdit.toPlainText()
rmessage = rmessage.replace("#>","")
rmsg = nick + " #> " + rmessage
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
c.connect((ip_address, 10420))
except Exception:
msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
return
try:
c.send(rmsg)
self.listWidget.addItem(rmsg)
self.textEdit.setText("")
except Exception:
msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
c.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I try to make chat app base on https://null-byte.wonderhowto.com/how-to/sploit-build-peer-peer-chat-application-python-gui-linux-0163617/
And get error
Exception in thread Thread-1: Traceback (most recent call last):
File "D:Program Filespython 3.7libthreading.py", line 917, in_bootstrap_inner
self.run()
File "D:Program Filespython 3.7libthreading.py", line 865, in run
self._target(*self._args, **self._kwargs)
File "chatbox.py", line 47, in server_socket
update_list(self, data)
File "chatbox.py", line 24, in update_list
self.listWidget.addItem(data)
TypeError: arguments did not match any overloaded call:
addItem(self, QListWidgetItem): argument 1 has unexpected type 'bytes'
addItem(self, str): argument 1 has unexpected type 'bytes'
I just begin code, so please forgive if having mistake
python pyqt pyqt5
python pyqt pyqt5
edited Nov 22 at 8:32
eyllanesc
71k93053
71k93053
asked Oct 31 at 0:46
Quang Cương Nguyễn
83
83
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
what is received through recv()
are bytes but QListWidget
requires string so the solution is to convert it using decode()
.
But your code has another hidden error(it seems to me that the author of the tutorial knows little about GUI so I recommend looking for another tutorial), the GUI can not be updated from another thread, that is forbidden by Qt, so the solution is to use signals or as in this case use @pyqtSlot()
with QMetaObject::invokeMethod()
.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'chatbox.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore,QtGui,QtWidgets
import sys
import socket
import threading
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 646)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(80, 50, 671, 31))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(70, 0, 331, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 0, 61, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(440, 0, 55, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 0, 171, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(80, 90, 401, 411))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.textEdit = QtWidgets.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.textEdit.setObjectName("textEdit")
self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(20, 380, 151, 28))
self.pushButton_3.setObjectName("pushButton_3")
#############################################################
# Executes When The Send Message Button Is Clicked
self.pushButton_3.clicked.connect(self.client_send_message)
############################################################
self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(230, 380, 151, 28))
self.pushButton_4.setObjectName("pushButton_4")
#############################################################
# Executes When The Clear Logs Button Is Clicked
self.pushButton_4.clicked.connect(self.clear_logs)
##############################################################
self.frame_3 = QtWidgets.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(490, 80, 261, 421))
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.listWidget = QtWidgets.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 241, 401))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 860, 26))
self.menubar.setObjectName("menubar")
self.menuMenu_Action = QtWidgets.QMenu(self.menubar)
self.menuMenu_Action.setObjectName("menuMenu_Action")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionVersion = QtWidgets.QAction(MainWindow)
self.actionVersion.setObjectName("actionVersion")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "IP address"))
self.label_2.setText(_translate("MainWindow", "Name"))
self.pushButton_3.setText(_translate("MainWindow", "Send"))
self.pushButton_4.setText(_translate("MainWindow", "Clear logs"))
self.menuMenu_Action.setTitle(_translate("MainWindow", "Menu Action"))
self.actionVersion.setText(_translate("MainWindow", "Version"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#######################################################
# Executes When The SubMenu Item Version Is Clicked
self.actionVersion.triggered.connect(self.app_version)
#######################################################
#######################################################
# Executes When The SubMenu Item Exit Is Clicked
self.actionExit.triggered.connect(QtWidgets.qApp.quit)
#######################################################
self.menuMenu_Action.addAction(self.actionVersion)
self.menuMenu_Action.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Action.menuAction())
self.start_server()
def clear_logs(self):
self.listWidget.clear()
def start_server(self):
threading.Thread(target=server_socket, args=(self,)).start()
self.msg_box("Success", "Server Started Sucessfully")
def client_send_message(self):
ip_address = self.lineEdit.text()
nick = self.lineEdit_2.text()
nick = nick.replace("#>","")
rmessage = self.textEdit.toPlainText()
rmessage = rmessage.replace("#>","")
rmsg = nick + " #> " + rmessage
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
c.connect((ip_address, 10420))
except Exception:
self.msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
return
try:
c.send(rmsg.encode())
self.listWidget.addItem(rmsg)
self.textEdit.clear()
except Exception:
self.msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
c.close()
def app_version(self):
self.msg_box("Application Version", "Test 0.1")
@QtCore.pyqtSlot(str, str)
def msg_box(self, title, data):
QtWidgets.QMessageBox.information(None, title, data)
@QtCore.pyqtSlot(str)
def update_list(self, data):
self.listWidget.addItem(data)
print("a")
def server_socket(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 10420))
s.listen(1)
except socket.error:
QtCore.QMetaObject.invokeMethod(self, "msg_box", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str, "Socket Error !!"),
QtCore.Q_ARG(str, "Unable To Setup Local Socket. Port In Use"))
return
while True:
conn, addr = s.accept()
incoming_ip = str(addr[0])
current_chat_ip = self.lineEdit.text()
if incoming_ip != current_chat_ip:
conn.close()
else:
data = conn.recv(4096).decode()
QtCore.QMetaObject.invokeMethod(self, "update_list", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str,data))
conn.close()
s.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
On the other hand I recommend using QTcpSocket since with its use there is no need for threads, in the this link you can find several examples.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
what is received through recv()
are bytes but QListWidget
requires string so the solution is to convert it using decode()
.
But your code has another hidden error(it seems to me that the author of the tutorial knows little about GUI so I recommend looking for another tutorial), the GUI can not be updated from another thread, that is forbidden by Qt, so the solution is to use signals or as in this case use @pyqtSlot()
with QMetaObject::invokeMethod()
.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'chatbox.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore,QtGui,QtWidgets
import sys
import socket
import threading
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 646)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(80, 50, 671, 31))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(70, 0, 331, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 0, 61, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(440, 0, 55, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 0, 171, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(80, 90, 401, 411))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.textEdit = QtWidgets.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.textEdit.setObjectName("textEdit")
self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(20, 380, 151, 28))
self.pushButton_3.setObjectName("pushButton_3")
#############################################################
# Executes When The Send Message Button Is Clicked
self.pushButton_3.clicked.connect(self.client_send_message)
############################################################
self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(230, 380, 151, 28))
self.pushButton_4.setObjectName("pushButton_4")
#############################################################
# Executes When The Clear Logs Button Is Clicked
self.pushButton_4.clicked.connect(self.clear_logs)
##############################################################
self.frame_3 = QtWidgets.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(490, 80, 261, 421))
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.listWidget = QtWidgets.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 241, 401))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 860, 26))
self.menubar.setObjectName("menubar")
self.menuMenu_Action = QtWidgets.QMenu(self.menubar)
self.menuMenu_Action.setObjectName("menuMenu_Action")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionVersion = QtWidgets.QAction(MainWindow)
self.actionVersion.setObjectName("actionVersion")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "IP address"))
self.label_2.setText(_translate("MainWindow", "Name"))
self.pushButton_3.setText(_translate("MainWindow", "Send"))
self.pushButton_4.setText(_translate("MainWindow", "Clear logs"))
self.menuMenu_Action.setTitle(_translate("MainWindow", "Menu Action"))
self.actionVersion.setText(_translate("MainWindow", "Version"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#######################################################
# Executes When The SubMenu Item Version Is Clicked
self.actionVersion.triggered.connect(self.app_version)
#######################################################
#######################################################
# Executes When The SubMenu Item Exit Is Clicked
self.actionExit.triggered.connect(QtWidgets.qApp.quit)
#######################################################
self.menuMenu_Action.addAction(self.actionVersion)
self.menuMenu_Action.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Action.menuAction())
self.start_server()
def clear_logs(self):
self.listWidget.clear()
def start_server(self):
threading.Thread(target=server_socket, args=(self,)).start()
self.msg_box("Success", "Server Started Sucessfully")
def client_send_message(self):
ip_address = self.lineEdit.text()
nick = self.lineEdit_2.text()
nick = nick.replace("#>","")
rmessage = self.textEdit.toPlainText()
rmessage = rmessage.replace("#>","")
rmsg = nick + " #> " + rmessage
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
c.connect((ip_address, 10420))
except Exception:
self.msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
return
try:
c.send(rmsg.encode())
self.listWidget.addItem(rmsg)
self.textEdit.clear()
except Exception:
self.msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
c.close()
def app_version(self):
self.msg_box("Application Version", "Test 0.1")
@QtCore.pyqtSlot(str, str)
def msg_box(self, title, data):
QtWidgets.QMessageBox.information(None, title, data)
@QtCore.pyqtSlot(str)
def update_list(self, data):
self.listWidget.addItem(data)
print("a")
def server_socket(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 10420))
s.listen(1)
except socket.error:
QtCore.QMetaObject.invokeMethod(self, "msg_box", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str, "Socket Error !!"),
QtCore.Q_ARG(str, "Unable To Setup Local Socket. Port In Use"))
return
while True:
conn, addr = s.accept()
incoming_ip = str(addr[0])
current_chat_ip = self.lineEdit.text()
if incoming_ip != current_chat_ip:
conn.close()
else:
data = conn.recv(4096).decode()
QtCore.QMetaObject.invokeMethod(self, "update_list", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str,data))
conn.close()
s.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
On the other hand I recommend using QTcpSocket since with its use there is no need for threads, in the this link you can find several examples.
add a comment |
up vote
0
down vote
accepted
what is received through recv()
are bytes but QListWidget
requires string so the solution is to convert it using decode()
.
But your code has another hidden error(it seems to me that the author of the tutorial knows little about GUI so I recommend looking for another tutorial), the GUI can not be updated from another thread, that is forbidden by Qt, so the solution is to use signals or as in this case use @pyqtSlot()
with QMetaObject::invokeMethod()
.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'chatbox.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore,QtGui,QtWidgets
import sys
import socket
import threading
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 646)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(80, 50, 671, 31))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(70, 0, 331, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 0, 61, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(440, 0, 55, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 0, 171, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(80, 90, 401, 411))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.textEdit = QtWidgets.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.textEdit.setObjectName("textEdit")
self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(20, 380, 151, 28))
self.pushButton_3.setObjectName("pushButton_3")
#############################################################
# Executes When The Send Message Button Is Clicked
self.pushButton_3.clicked.connect(self.client_send_message)
############################################################
self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(230, 380, 151, 28))
self.pushButton_4.setObjectName("pushButton_4")
#############################################################
# Executes When The Clear Logs Button Is Clicked
self.pushButton_4.clicked.connect(self.clear_logs)
##############################################################
self.frame_3 = QtWidgets.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(490, 80, 261, 421))
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.listWidget = QtWidgets.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 241, 401))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 860, 26))
self.menubar.setObjectName("menubar")
self.menuMenu_Action = QtWidgets.QMenu(self.menubar)
self.menuMenu_Action.setObjectName("menuMenu_Action")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionVersion = QtWidgets.QAction(MainWindow)
self.actionVersion.setObjectName("actionVersion")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "IP address"))
self.label_2.setText(_translate("MainWindow", "Name"))
self.pushButton_3.setText(_translate("MainWindow", "Send"))
self.pushButton_4.setText(_translate("MainWindow", "Clear logs"))
self.menuMenu_Action.setTitle(_translate("MainWindow", "Menu Action"))
self.actionVersion.setText(_translate("MainWindow", "Version"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#######################################################
# Executes When The SubMenu Item Version Is Clicked
self.actionVersion.triggered.connect(self.app_version)
#######################################################
#######################################################
# Executes When The SubMenu Item Exit Is Clicked
self.actionExit.triggered.connect(QtWidgets.qApp.quit)
#######################################################
self.menuMenu_Action.addAction(self.actionVersion)
self.menuMenu_Action.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Action.menuAction())
self.start_server()
def clear_logs(self):
self.listWidget.clear()
def start_server(self):
threading.Thread(target=server_socket, args=(self,)).start()
self.msg_box("Success", "Server Started Sucessfully")
def client_send_message(self):
ip_address = self.lineEdit.text()
nick = self.lineEdit_2.text()
nick = nick.replace("#>","")
rmessage = self.textEdit.toPlainText()
rmessage = rmessage.replace("#>","")
rmsg = nick + " #> " + rmessage
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
c.connect((ip_address, 10420))
except Exception:
self.msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
return
try:
c.send(rmsg.encode())
self.listWidget.addItem(rmsg)
self.textEdit.clear()
except Exception:
self.msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
c.close()
def app_version(self):
self.msg_box("Application Version", "Test 0.1")
@QtCore.pyqtSlot(str, str)
def msg_box(self, title, data):
QtWidgets.QMessageBox.information(None, title, data)
@QtCore.pyqtSlot(str)
def update_list(self, data):
self.listWidget.addItem(data)
print("a")
def server_socket(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 10420))
s.listen(1)
except socket.error:
QtCore.QMetaObject.invokeMethod(self, "msg_box", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str, "Socket Error !!"),
QtCore.Q_ARG(str, "Unable To Setup Local Socket. Port In Use"))
return
while True:
conn, addr = s.accept()
incoming_ip = str(addr[0])
current_chat_ip = self.lineEdit.text()
if incoming_ip != current_chat_ip:
conn.close()
else:
data = conn.recv(4096).decode()
QtCore.QMetaObject.invokeMethod(self, "update_list", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str,data))
conn.close()
s.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
On the other hand I recommend using QTcpSocket since with its use there is no need for threads, in the this link you can find several examples.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
what is received through recv()
are bytes but QListWidget
requires string so the solution is to convert it using decode()
.
But your code has another hidden error(it seems to me that the author of the tutorial knows little about GUI so I recommend looking for another tutorial), the GUI can not be updated from another thread, that is forbidden by Qt, so the solution is to use signals or as in this case use @pyqtSlot()
with QMetaObject::invokeMethod()
.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'chatbox.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore,QtGui,QtWidgets
import sys
import socket
import threading
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 646)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(80, 50, 671, 31))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(70, 0, 331, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 0, 61, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(440, 0, 55, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 0, 171, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(80, 90, 401, 411))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.textEdit = QtWidgets.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.textEdit.setObjectName("textEdit")
self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(20, 380, 151, 28))
self.pushButton_3.setObjectName("pushButton_3")
#############################################################
# Executes When The Send Message Button Is Clicked
self.pushButton_3.clicked.connect(self.client_send_message)
############################################################
self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(230, 380, 151, 28))
self.pushButton_4.setObjectName("pushButton_4")
#############################################################
# Executes When The Clear Logs Button Is Clicked
self.pushButton_4.clicked.connect(self.clear_logs)
##############################################################
self.frame_3 = QtWidgets.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(490, 80, 261, 421))
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.listWidget = QtWidgets.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 241, 401))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 860, 26))
self.menubar.setObjectName("menubar")
self.menuMenu_Action = QtWidgets.QMenu(self.menubar)
self.menuMenu_Action.setObjectName("menuMenu_Action")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionVersion = QtWidgets.QAction(MainWindow)
self.actionVersion.setObjectName("actionVersion")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "IP address"))
self.label_2.setText(_translate("MainWindow", "Name"))
self.pushButton_3.setText(_translate("MainWindow", "Send"))
self.pushButton_4.setText(_translate("MainWindow", "Clear logs"))
self.menuMenu_Action.setTitle(_translate("MainWindow", "Menu Action"))
self.actionVersion.setText(_translate("MainWindow", "Version"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#######################################################
# Executes When The SubMenu Item Version Is Clicked
self.actionVersion.triggered.connect(self.app_version)
#######################################################
#######################################################
# Executes When The SubMenu Item Exit Is Clicked
self.actionExit.triggered.connect(QtWidgets.qApp.quit)
#######################################################
self.menuMenu_Action.addAction(self.actionVersion)
self.menuMenu_Action.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Action.menuAction())
self.start_server()
def clear_logs(self):
self.listWidget.clear()
def start_server(self):
threading.Thread(target=server_socket, args=(self,)).start()
self.msg_box("Success", "Server Started Sucessfully")
def client_send_message(self):
ip_address = self.lineEdit.text()
nick = self.lineEdit_2.text()
nick = nick.replace("#>","")
rmessage = self.textEdit.toPlainText()
rmessage = rmessage.replace("#>","")
rmsg = nick + " #> " + rmessage
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
c.connect((ip_address, 10420))
except Exception:
self.msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
return
try:
c.send(rmsg.encode())
self.listWidget.addItem(rmsg)
self.textEdit.clear()
except Exception:
self.msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
c.close()
def app_version(self):
self.msg_box("Application Version", "Test 0.1")
@QtCore.pyqtSlot(str, str)
def msg_box(self, title, data):
QtWidgets.QMessageBox.information(None, title, data)
@QtCore.pyqtSlot(str)
def update_list(self, data):
self.listWidget.addItem(data)
print("a")
def server_socket(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 10420))
s.listen(1)
except socket.error:
QtCore.QMetaObject.invokeMethod(self, "msg_box", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str, "Socket Error !!"),
QtCore.Q_ARG(str, "Unable To Setup Local Socket. Port In Use"))
return
while True:
conn, addr = s.accept()
incoming_ip = str(addr[0])
current_chat_ip = self.lineEdit.text()
if incoming_ip != current_chat_ip:
conn.close()
else:
data = conn.recv(4096).decode()
QtCore.QMetaObject.invokeMethod(self, "update_list", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str,data))
conn.close()
s.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
On the other hand I recommend using QTcpSocket since with its use there is no need for threads, in the this link you can find several examples.
what is received through recv()
are bytes but QListWidget
requires string so the solution is to convert it using decode()
.
But your code has another hidden error(it seems to me that the author of the tutorial knows little about GUI so I recommend looking for another tutorial), the GUI can not be updated from another thread, that is forbidden by Qt, so the solution is to use signals or as in this case use @pyqtSlot()
with QMetaObject::invokeMethod()
.
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'chatbox.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore,QtGui,QtWidgets
import sys
import socket
import threading
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(860, 646)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(80, 50, 671, 31))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.lineEdit = QtWidgets.QLineEdit(self.frame)
self.lineEdit.setGeometry(QtCore.QRect(70, 0, 331, 22))
self.lineEdit.setObjectName("lineEdit")
self.label = QtWidgets.QLabel(self.frame)
self.label.setGeometry(QtCore.QRect(10, 0, 61, 21))
self.label.setObjectName("label")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(440, 0, 55, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.frame)
self.lineEdit_2.setGeometry(QtCore.QRect(490, 0, 171, 22))
self.lineEdit_2.setObjectName("lineEdit_2")
self.frame_2 = QtWidgets.QFrame(self.centralwidget)
self.frame_2.setGeometry(QtCore.QRect(80, 90, 401, 411))
self.frame_2.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_2.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_2.setObjectName("frame_2")
self.textEdit = QtWidgets.QTextEdit(self.frame_2)
self.textEdit.setGeometry(QtCore.QRect(10, 10, 381, 361))
self.textEdit.setObjectName("textEdit")
self.pushButton_3 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_3.setGeometry(QtCore.QRect(20, 380, 151, 28))
self.pushButton_3.setObjectName("pushButton_3")
#############################################################
# Executes When The Send Message Button Is Clicked
self.pushButton_3.clicked.connect(self.client_send_message)
############################################################
self.pushButton_4 = QtWidgets.QPushButton(self.frame_2)
self.pushButton_4.setGeometry(QtCore.QRect(230, 380, 151, 28))
self.pushButton_4.setObjectName("pushButton_4")
#############################################################
# Executes When The Clear Logs Button Is Clicked
self.pushButton_4.clicked.connect(self.clear_logs)
##############################################################
self.frame_3 = QtWidgets.QFrame(self.centralwidget)
self.frame_3.setGeometry(QtCore.QRect(490, 80, 261, 421))
self.frame_3.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame_3.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame_3.setObjectName("frame_3")
self.listWidget = QtWidgets.QListWidget(self.frame_3)
self.listWidget.setGeometry(QtCore.QRect(10, 10, 241, 401))
self.listWidget.setObjectName("listWidget")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 860, 26))
self.menubar.setObjectName("menubar")
self.menuMenu_Action = QtWidgets.QMenu(self.menubar)
self.menuMenu_Action.setObjectName("menuMenu_Action")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.actionVersion = QtWidgets.QAction(MainWindow)
self.actionVersion.setObjectName("actionVersion")
self.actionExit = QtWidgets.QAction(MainWindow)
self.actionExit.setObjectName("actionExit")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.label.setText(_translate("MainWindow", "IP address"))
self.label_2.setText(_translate("MainWindow", "Name"))
self.pushButton_3.setText(_translate("MainWindow", "Send"))
self.pushButton_4.setText(_translate("MainWindow", "Clear logs"))
self.menuMenu_Action.setTitle(_translate("MainWindow", "Menu Action"))
self.actionVersion.setText(_translate("MainWindow", "Version"))
self.actionExit.setText(_translate("MainWindow", "Exit"))
class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
super(MainWindow, self).__init__(parent)
self.setupUi(self)
#######################################################
# Executes When The SubMenu Item Version Is Clicked
self.actionVersion.triggered.connect(self.app_version)
#######################################################
#######################################################
# Executes When The SubMenu Item Exit Is Clicked
self.actionExit.triggered.connect(QtWidgets.qApp.quit)
#######################################################
self.menuMenu_Action.addAction(self.actionVersion)
self.menuMenu_Action.addAction(self.actionExit)
self.menubar.addAction(self.menuMenu_Action.menuAction())
self.start_server()
def clear_logs(self):
self.listWidget.clear()
def start_server(self):
threading.Thread(target=server_socket, args=(self,)).start()
self.msg_box("Success", "Server Started Sucessfully")
def client_send_message(self):
ip_address = self.lineEdit.text()
nick = self.lineEdit_2.text()
nick = nick.replace("#>","")
rmessage = self.textEdit.toPlainText()
rmessage = rmessage.replace("#>","")
rmsg = nick + " #> " + rmessage
c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
c.connect((ip_address, 10420))
except Exception:
self.msg_box("Connection Refused", "The Address You Are Trying To Reach Is Currently Unavailable")
return
try:
c.send(rmsg.encode())
self.listWidget.addItem(rmsg)
self.textEdit.clear()
except Exception:
self.msg_box("Connection Refused", "The Message Cannot Be Sent. End-Point Not Connected !!")
c.close()
def app_version(self):
self.msg_box("Application Version", "Test 0.1")
@QtCore.pyqtSlot(str, str)
def msg_box(self, title, data):
QtWidgets.QMessageBox.information(None, title, data)
@QtCore.pyqtSlot(str)
def update_list(self, data):
self.listWidget.addItem(data)
print("a")
def server_socket(self):
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 10420))
s.listen(1)
except socket.error:
QtCore.QMetaObject.invokeMethod(self, "msg_box", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str, "Socket Error !!"),
QtCore.Q_ARG(str, "Unable To Setup Local Socket. Port In Use"))
return
while True:
conn, addr = s.accept()
incoming_ip = str(addr[0])
current_chat_ip = self.lineEdit.text()
if incoming_ip != current_chat_ip:
conn.close()
else:
data = conn.recv(4096).decode()
QtCore.QMetaObject.invokeMethod(self, "update_list", QtCore.Qt.QueuedConnection, QtCore.Q_ARG(str,data))
conn.close()
s.close()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = MainWindow()
w.show()
sys.exit(app.exec_())
On the other hand I recommend using QTcpSocket since with its use there is no need for threads, in the this link you can find several examples.
answered Oct 31 at 1:30
eyllanesc
71k93053
71k93053
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53074766%2fpyqt-additem-typeerror-arguments-did-not-match-any-overloaded-call%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown