#1、无参数线程后台运行
self.thead1 = threading.Thread(target=self.hanshu_one, args=(self,))
self.thead1.start()
# hanshu_one就是需要后台运行的函数名字
2、
import time
from PyQt5.QtCore import QObject, pyqtSignal, QThread
from PyQt5.QtWidgets import QProgressBar, QApplication, QPushButton, QDialog, QGridLayout
#线程1--普通线程
class Test_Work_1(QThread):#继承自QThread
send_signal = pyqtSignal(int) #计划发送的信号
def __init__(self):#初始化
super(Test_Work_1, self).__init__()#父类初始化
def run(self): #定义函数
for i in range(101):#循环
#print(i)
self.send_signal.emit(i)#计划发送的信号
time.sleep(0.01)#休眠
#线程2move----用
class Test_Work_2(QObject):
send_signal = pyqtSignal(int) # #计划发送的信号
send_finished = pyqtSignal() #结束的信号
def __init__(self): #实例化时传入一些参数,以方便且正确-从最初就设置某些属性值,
#对继承自父类的属性进行初始化,self=自己
#先找到父类--父类的对象self转换为子类对象,--“被转换”的子类调用自己的__init__函数
super(Test_Work_2, self).__init__()#
def run(self):
for i in range(101):
print(i)
self.send_signal.emit(i)#
time.sleep(0.01)
self.send_finished.emit()
class ThreadDialog(QDialog):
def __init__(self):
super(ThreadDialog, self).__init__()
# MainWindow.resize(1313, 897)
self.grid_layout = QGridLayout(self)
self.grid_layout
self.progress = QProgressBar(self)
self.grid_layout.addWidget(self.progress, 0, 1, 1, 1)
self.pushbutton_one = QPushButton('方法1:继承线程', self)
self.grid_layout.addWidget(self.pushbutton_one, 2, 2, 1, 1)
self.pushbutton2 = QPushButton('方法2:movetothread', self)
self.grid_layout.addWidget(self.pushbutton2, 3, 3, 3, 3)
self.pushbutton_one.clicked.connect(self.pushbutton_one_clicked)
self.pushbutton2.clicked.connect(self.pushbutton2_clicked)
def pushbutton_one_clicked(self):
self.test_worker1 = Test_Work_1()#线程1实例化
#发送信号与进度条连接
self.test_worker1.send_signal.connect(self.progress.setValue)#
#线程开始,实现持续信号发送
self.test_worker1.start()
def pushbutton2_clicked(self):
self.test_worker2 = Test_Work_1()#实例化
self.test_thread2 = QThread()#实例化
self.test_worker2.moveToThread(self.test_thread2)#线程移送
self.test_worker2.send_signal.connect(self.progress.setValue)#设置信号发送--与进度条联系
self.test_worker2.send_finished.connect(self.test_worker2_finished)#设置信号--函数联系
self.test_thread2.started.connect(self.test_worker2.run)#设置QThread的开始--与实例run联系
self.test_thread2.start()#线程开始
def test_worker2_finished(self):
self.test_thread2.quit()
self.test_thread2.wait()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
thread_dialog = ThreadDialog()
thread_dialog.show()
app.exec_()
################################
# #线程每个几秒把一个数发送给这个函数(=或者类里的一个方法)
# self.newthread.signal_out.connect(self.DingDan_BianHao_build_test) # 连接线程类中自定义信号槽到本类的自定义槽函数
```python
self.newthread.start() # 开启线程不是调用run函数,而是调用start函数
# 采用多线程操作
self.model_process = model_thread()
# # 采用槽函数---把数值推送给连接的函数
self.model_process.model_signal_send.connect(self.DingDan_BianHao_build_ss)
# 启动线程
self.model_process.start()
# 使用args传参threading.Thread(target=sing, args=(10, 100, 100))
# 使用kwargs传参threading.Thread(target=sing, kwargs={“a”: 10, “b”:100, “c”: 100})
# 同时args和kwargs传参threading.Thread(target=sing, args=(10,), kwargs={“b”: 100,“c”: 100})
# self.thead1 = threading.Thread(target=self.DingDan_BianHao_build_ss, args=(self,))
################################
> https://blog.csdn.net/qq_36044523/article/details/108849365