您好,登录后才能下订单哦!
这篇文章主要介绍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线程中无法运行怎么办”这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注亿速云行业资讯频道!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。