pyqt5动画在QThread线程中无法运行怎么办-创新互联
这篇文章主要介绍pyqt5动画在QThread线程中无法运行怎么办,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
创新互联是一家集网站建设,良庆企业网站建设,良庆品牌网站建设,网站定制,良庆网站建设报价,网络营销,网络优化,良庆网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。自己做了一个tcp工具,在学习动画的时候踩了坑,需求是根据上线变绿色,离线变灰色,如果连接断开了,则变为灰色
问题现象:
可以看到点击“连接”,“离线”的时候动画是正常的,但是当tcp超时断开后,虽然离线按钮变为连接了,却没有执行离线动画
关键源代码如下
class BSJTcpThread(QtCore.QThread): recv_signal = QtCore.pyqtSignal(str) send_signal = QtCore.pyqtSignal(str) def __init__(self, socketcp, onBtn, heartcheck, senBtn, scene): super().__init__() self.s = socketcp self.yqtool = Bianlifunction() self.onBtn = onBtn self.heartcheck = heartcheck self.sendBtn = senBtn self.scene1 = scene def run(self): """线程""" global stopsingle stopsingle = 0 while 1: btcpreceive = self.s.recv(1024) tcpreceive1 = str(binascii.b2a_hex(btcpreceive), encoding="utf-8") tcpreceive = "" i = 0 while i < len(tcpreceive1) - 1: # 十六进制数据处理,两个字节隔开 if i == len(tcpreceive1) - 2: tcpreceive += tcpreceive1[i:i + 2] i += 2 else: tcpreceive += tcpreceive1[i:i + 2] + " " i += 2 if tcpreceive == "": stopsingle = 1 self.s.shutdown(2) self.s.close() self.onBtn.setText("连接") self.scene1.offlineCol.start() # 启动离线动画 self.heartcheck.setChecked(False) self.heartcheck.setVisible(False) self.sendBtn.setDisabled(True) else: self.recv_signal.emit(tcpreceive) if stopsingle == 1: break
然后再启动线程
self.tcpth = BSJTcpThread(self.s, self.onBtn, self.heartcheck, self.sendBtn, self.scene) self.tcpth.recv_signal.connect(self.fillrecvmsg) self.tcpth.send_signal.connect(self.fillsendmsg) self.tcpth.start()
问题点:
经过谷爹搜索,终于找到了问题原因详见https://stackoverflow.com/questions/44328750/pyqt-qgraphicscene-move-item-in-background-thread
大致原因就是QGraphics Scene 不是一个安全的线程对象,我们不能直接在线程中去改变主程序的状态,我们必须通过信号的方式去更新QGraphics
解决方法:
首先,我们编辑一个信号方法
def threadAnimate(self, message): if message == "1": self.scene.offlineCol.start()
然后添加相关信号槽
self.tcpth = BSJTcpThread(self.s, self.onBtn, self.heartcheck, self.sendBtn) self.tcpth.recv_signal.connect(self.fillrecvmsg) self.tcpth.send_signal.connect(self.fillsendmsg) self.tcpth.animate_signal.connect(self.threadAnimate) # 添加一个动画信号 self.tcpth.start()
在线程中发出离线动画的信号
class BSJTcpThread(QtCore.QThread): recv_signal = QtCore.pyqtSignal(str) send_signal = QtCore.pyqtSignal(str) animate_signal = QtCore.pyqtSignal(str) def __init__(self, socketcp, onBtn, heartcheck, senBtn): super().__init__() self.s = socketcp self.yqtool = Bianlifunction() self.onBtn = onBtn self.heartcheck = heartcheck self.sendBtn = senBtn def run(self): """线程""" global stopsingle stopsingle = 0 while 1: btcpreceive = self.s.recv(1024) tcpreceive1 = str(binascii.b2a_hex(btcpreceive), encoding="utf-8") tcpreceive = "" i = 0 while i < len(tcpreceive1) - 1: # 十六进制数据处理,两个字节隔开 if i == len(tcpreceive1) - 2: tcpreceive += tcpreceive1[i:i + 2] i += 2 else: tcpreceive += tcpreceive1[i:i + 2] + " " i += 2 if tcpreceive == "": stopsingle = 1 self.s.shutdown(2) self.s.close() self.onBtn.setText("连接") self.animate_signal.emit("1") self.heartcheck.setChecked(False) self.heartcheck.setVisible(False) self.sendBtn.setDisabled(True) else: self.recv_signal.emit(tcpreceive) if stopsingle == 1: break
然后就可以了,这个和QThread多线程收发消息原理一样
以上是“pyqt5动画在QThread线程中无法运行怎么办”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注创新互联行业资讯频道!
本文题目:pyqt5动画在QThread线程中无法运行怎么办-创新互联
文章出自:http://lswzjz.com/article/dcjoih.html