您好,登錄后才能下訂單哦!
這篇文章主要介紹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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。