【发布时间】:2019-12-29 07:08:26
【问题描述】:
在下面的代码中,我有一个类 The Thread,它继承自 threading 以在不同的线程中执行任务,在这种情况下,任务是在 QLabel 中显示 .gif 图像,同时执行另一个任务。
在这种情况下,当发出执行 Finish() 函数的信号时,tabWidget() 小部件从 index 变为 1
但是当我们返回索引 0 时,我们可以看到 .gif 仍在 QLabel 中运行。
所以我想知道如何停止该线程的执行, 我试过了
TheThread.kill()
TheThread.stop()
但它不起作用,这是带有.ui文件和image.gif的完整代码
from PyQt5.QtWidgets import QMainWindow,QApplication,QTabWidget,QPushButton,QLabel
from PyQt5 import QtCore,QtGui
from PyQt5 import uic
import threading
import time
class TheThread(threading.Thread):
def __init__(self,obj,fun):
threading.Thread.__init__(self)
self.obj = obj
self.fun = fun
def run(self):
self.fun()
class Main(QMainWindow):
signal = QtCore.pyqtSignal(object)
def __init__(self):
QMainWindow.__init__(self)
uic.loadUi("Test.ui",self)
self.Iniciar.clicked.connect(lambda:self.ShowImage())
self.signal.connect(self.Finish)
self._thread = TheThread(self,self.Fun1)
def ShowImage(self):
_movie = QtGui.QMovie("Loader.gif")
self.Animacion.setMovie(_movie)
self.Animacion.setScaledContents(True)
_movie.start()
self._thread.start()
def Fun1(self):
time.sleep(3)
self.signal.emit(0)
def Finish(self,signal):
if signal == 0:
time.sleep(1)
self.tabWidget.setCurrentIndex(1)
# TheThread.kill()
TheThread.stop()
app = QApplication([])
m = Main()
m.show()
app.exec_()
文件.ui
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>MainWindow</class>
<widget class="QMainWindow" name="MainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>600</height>
</rect>
</property>
<property name="windowTitle">
<string>MainWindow</string>
</property>
<widget class="QWidget" name="centralwidget">
<widget class="QTabWidget" name="tabWidget">
<property name="geometry">
<rect>
<x>30</x>
<y>0</y>
<width>691</width>
<height>441</height>
</rect>
</property>
<widget class="QWidget" name="tab">
<attribute name="title">
<string>Tab 1</string>
</attribute>
<widget class="QLabel" name="Animacion">
<property name="geometry">
<rect>
<x>220</x>
<y>40</y>
<width>231</width>
<height>171</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
<widget class="QLabel" name="Estatus">
<property name="geometry">
<rect>
<x>220</x>
<y>270</y>
<width>271</width>
<height>16</height>
</rect>
</property>
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</widget>
<widget class="QWidget" name="tab_2">
<attribute name="title">
<string>Tab 2</string>
</attribute>
</widget>
</widget>
<widget class="QPushButton" name="Iniciar">
<property name="geometry">
<rect>
<x>30</x>
<y>460</y>
<width>75</width>
<height>23</height>
</rect>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>800</width>
<height>21</height>
</rect>
</property>
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<resources/>
<connections/>
</ui>
【问题讨论】:
-
尝试杀死线程对象,而不是它的类:
self._thread.stop ()。注意:还有一些地方需要改进
标签: python multithreading pyqt5