這篇文章主要介紹pyqt5動(dòng)畫(huà)在QThread線程中無(wú)法運(yùn)行怎么辦,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),良慶企業(yè)網(wǎng)站建設(shè),良慶品牌網(wǎng)站建設(shè),網(wǎng)站定制,良慶網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷(xiāo),網(wǎng)絡(luò)優(yōu)化,良慶網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。自己做了一個(gè)tcp工具,在學(xué)習(xí)動(dòng)畫(huà)的時(shí)候踩了坑,需求是根據(jù)上線變綠色,離線變灰色,如果連接斷開(kāi)了,則變?yōu)榛疑?/p>
問(wèn)題現(xiàn)象:
可以看到點(diǎn)擊“連接”,“離線”的時(shí)候動(dòng)畫(huà)是正常的,但是當(dāng)tcp超時(shí)斷開(kāi)后,雖然離線按鈕變?yōu)檫B接了,卻沒(méi)有執(zhí)行離線動(dòng)畫(huà)
關(guān)鍵源代碼如下
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: # 十六進(jìn)制數(shù)據(jù)處理,兩個(gè)字節(jié)隔開(kāi) 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() # 啟動(dòng)離線動(dòng)畫(huà) self.heartcheck.setChecked(False) self.heartcheck.setVisible(False) self.sendBtn.setDisabled(True) else: self.recv_signal.emit(tcpreceive) if stopsingle == 1: break
然后再啟動(dòng)線程
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()
問(wèn)題點(diǎn):
經(jīng)過(guò)谷爹搜索,終于找到了問(wèn)題原因詳見(jiàn)https://stackoverflow.com/questions/44328750/pyqt-qgraphicscene-move-item-in-background-thread
大致原因就是QGraphics Scene 不是一個(gè)安全的線程對(duì)象,我們不能直接在線程中去改變主程序的狀態(tài),我們必須通過(guò)信號(hào)的方式去更新QGraphics
解決方法:
首先,我們編輯一個(gè)信號(hào)方法
def threadAnimate(self, message): if message == "1": self.scene.offlineCol.start()
然后添加相關(guān)信號(hào)槽
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) # 添加一個(gè)動(dòng)畫(huà)信號(hào) self.tcpth.start()
在線程中發(fā)出離線動(dòng)畫(huà)的信號(hào)
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: # 十六進(jìn)制數(shù)據(jù)處理,兩個(gè)字節(jié)隔開(kāi) 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
然后就可以了,這個(gè)和QThread多線程收發(fā)消息原理一樣
以上是“pyqt5動(dòng)畫(huà)在QThread線程中無(wú)法運(yùn)行怎么辦”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!