我用pyqt4和python3.3中的线程编写一个简单的套接字应用程序,但我在运行Qt classes命令时遇到问题,比如在标签或任何事情中显示消息,只有python代码正常工作。
在我的gui中没有向S_label显示任何内容,
我使用2种方法在S_label上显示任何消息,但两种方式都不起作用,
代码:
对于print
这两种方式都很有效:
print
输出:
- <imigui.Ui_MainWindow object at 0x7fe0746cac10>
- <imigui.Ui_MainWindow object at 0x7fe0746d5d90>
我做错了什么?
我的代码:
from PyQt4 import QtCore, QtGui
from imigui import Ui_MainWindow
class imiserv(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Sport_lineEdit.setMaxLength(5)
self.ui.Sconnect_pushButton.clicked.connect(self.serv)
def serv(self):
self.sersock= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sersock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.host= str(self.ui.Sip_lineEdit.text())
self.port= int(self.ui.Sport_lineEdit.text())
MY_LOCK = threading.Lock()
class CountT(threading.Thread, imiserv):
def __init__(self, ui, ser):
super().__init__()
imiserv.__init__(self)
self._sersock= ser
self._ui= ui
print(self._ui)
print(self.ui)
self.ui.S_label.setText("test !")
self._ui.S_label.setText("test !")
def run(self):
MY_LOCK.acquire()
while True:
cliconn, (addr, remoport)= self._sersock.accept()
self.cstr= cliconn.recv(1024)
self.ui.S_label.setText("{0}:{1} is connected.".format(addr, remoport))
cliconn.close()
print(self.cstr)
MY_LOCK.release()
try :
self.sersock.bind((self.host, self.port))
self.sersock.listen(5)
a= CountT(self.ui, self.sersock)
a.daemon= True # for exit from thread when close gui
a.start()
except socket.error as err:
self.ui.S_label.setText(err)
编辑(新问题):
我的方式遇到了一个新问题:我在我的gui中添加了一个qtextedit,当我想在我的代码Segmentation fault
中设置文本时,发生了应用程序崩溃,
错误:
QObject: Cannot create children for a parent that is in a different thread.
(Parent is QTextDocument(0x15c4460), parent's thread is QThread(0xfeb570), current thread is QThread(0x15d3900)
Segmentation fault
我的代码:
from PyQt4 import QtCore, QtGui
from imigui import Ui_MainWindow
class imiserv(QtGui.QMainWindow):
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Sport_lineEdit.setMaxLength(5)
self.ui.Sconnect_pushButton.clicked.connect(self.serv)
def serv(self):
self.sersock= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sersock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.host= str(self.ui.Sip_lineEdit.text())
self.port= int(self.ui.Sport_lineEdit.text())
MY_LOCK = threading.Lock()
class CountT(threading.Thread):
def __init__(self, label, ser, qtext):
super().__init__()
self._sersock= ser
self._uilabel= label
self._uiClog= qtext
def run(self):
MY_LOCK.acquire()
while True:
cliconn, (addr, remoport)= self._sersock.accept()
self.cstr= cliconn.recv(1024)
self._uilabel.setText("{0}:{1} is connected.".format(addr, remoport))
cliconn.close()
print(self.cstr)
self._uilabel.setText(self.cstr) # it's ok!
self._uiClog.setText(msg) # Segmentation fault
MY_LOCK.release()
try :
self.sersock.bind((self.host, self.port))
self.sersock.listen(5)
a= CountT(label= self.ui.S_label, ser= self.sersock, qtext= self.ui.Clog_textEdit)
a.daemon= True # for exit from thread when close gui
a.start()
self.ui.S_label.setText("Connected !")
except socket.error as err:
self.ui.S_label.setText(err) # it's ok !
self.ui.self.ui.Clog_textEdit.setText(err) # it's ok !
为什么?
编辑(新问题2):
这是我的代码:
从PyQt4导入QtCore,QtGui 来自imigui import Ui_MainWindow
class imiserv(QtGui.QMainWindow):
send_msg = pyqtSignal('QString', 'QString')
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Sport_lineEdit.setMaxLength(5)
self.ui.Sconnect_pushButton.clicked.connect(self.serv)
self.send_msg.connect(self.write_msg)
def write_msg(self, lbl_msg= None, txt_msg= None):
if lbl_msg:
self.ui.C_label.setText(lbl_msg)
if txt_msg:
self.ui.Clog_textEdit.setText(txt_msg)
def serv(self):
MY_LOCK = threading.Lock()
class CountT(threading.Thread):
def __init__(self, parent):
threading.Thread.__init__(self)
self._parent= parent
def run(self):
MY_LOCK.acquire()
self._parent.send_msg.emit("Waiting connections","")
while True:
cliconn, (addr, remoport)= self._parent.clis.accept()
clirecmsg= str(cliconn.recv(1024)
self._parent.send_msg.emit("{0}:{1} is connected.".format(addr, remoport), "{0}:{1}".format(addr, remoport)
cliconn.close()
MY_LOCK.release()
try :
self.clis= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.clis.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
clierhost= str(self.ui.Sip_lineEdit.text())
clierport= int(self.ui.Sport_lineEdit.text())
self.clis.bind((clierhost, clierport))
self.clis.listen(5)
a= CountT(self)
a.daemon= True
a.start()
except socket.error as err:
err= str(err)
print(err)
这是发生交错的错误,我应该按两次按钮获取第一次获取事件错误在linux中显示并且没有任何内容在Windows和第二次按下事件发生,(此错误仅在linux操作系统中显示。)
Exception in thread Thread-1:
Traceback (most recent call last):
File "/usr/lib/python3.3/threading.py", line 637, in _bootstrap_inner
self.run()
File "imiclilap.py", line 34, in run
cliconn, (addr, remoport)= self._parent.clis.accept()
File "/usr/lib/python3.3/socket.py", line 135, in accept
fd, addr = self._accept()
OSError: [Errno 22] Invalid argument
Exception in thread Thread-2:
Traceback (most recent call last):
File "/usr/lib/python3.3/threading.py", line 637, in _bootstrap_inner
self.run()
File "imiclilap.py", line 34, in run
cliconn, (addr, remoport)= self._parent.clis.accept()
File "/usr/lib/python3.3/socket.py", line 135, in accept
fd, addr = self._accept()
OSError: [Errno 22] Invalid argument
答案 0 :(得分:1)
以下代码适用于我(Python 2.6)。我使用CountT
删除了imiserv
的继承,并将替换为{del> QLabel S_label
imiserv
实例。 pyqtsignal
允许线程CounT将文本写入QLabel和QTextEdit:
import socket
import threading
from PyQt4 import QtCore, QtGui
from imigui import Ui_MainWindow
class imiserv(QtGui.QMainWindow):
send_msg = pyqtSignal('QString', 'QString')
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.Sport_lineEdit.setMaxLength(5)
self.ui.Sconnect_pushButton.clicked.connect(self.serv)
self.send_msg.connect(self.my_slot)
def write_msg(self, lbl_msg, txt_msg):
self.ui.S_label.setText(lbl_msg)
self.ui.Clog_textEdit.setText(txt_msg)
def serv(self):
self.sersock= socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sersock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.host= str(self.ui.Sip_lineEdit.text())
self.port= int(self.ui.Sport_lineEdit.text())
MY_LOCK = threading.Lock()
class CountT(threading.Thread):
def __init__(self, parent):
threading.Thread.__init__(self)
self._parent= parent
def run(self):
MY_LOCK.acquire()
self._parent.send_msg.emit("Waiting connections","")
while True:
cliconn, (addr, remoport)= self.__parent.sersock.accept()
self.cstr= cliconn.recv(1024)
self._parent.send_msg.emit("{0}:{1} is connected.".format(addr, remoport), self.cstr)
cliconn.close()
MY_LOCK.release()
try :
self.sersock.bind((self.host, self.port))
self.sersock.listen(5)
a= CountT(self)
a.daemon= True # for exit from thread when close gui
a.start()
except socket.error as err:
self.ui.S_label.setText(err.strerror.decode("utf-8"))
self.ui.Clog_textEdit.setText(err.strerror.decode("utf-8"))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
s = imiserv()
s.show()
sys.exit(app.exec_())