学习PyQT5时,例子程序:Chapter07:qt07_signalSlotThreaad
程序运行没问题,但是运行以后,关闭或者stop,均报错:
RuntimeError: wrapped C/C++ object of type QTimer has been deleted
程序代码:
#-*- encoding: gb2312 -*-
import string, threading, time
from PyQt5.QtCore import QThread , pyqtSignal, QDateTime
from PyQt5.QtWidgets import QApplication, QDialog, QLineEdit
import time
import sys
def thread_main(a):
global count, mutex
# 获得线程名
threadname = threading.currentThread().getName()
for x in range(0, int(a)):
# 取得锁
mutex.acquire()
count = count + 1
# 释放锁
mutex.release()
print (threadname, x, count)
time.sleep(1)
def showthetime():
while True:
data = QDateTime.currentDateTime()
currTime = data.toString("yyyy-MM-dd hh:mm:ss")
#self.update_date.emit( str(currTime) )
time.sleep(1)
print(currTime)
def main(num):
global count, mutex
threads = []
count = 1
# 创建一个锁
mutex = threading.Lock()
# 先创建线程对象
for x in range(0, num):
threads.append(threading.Thread(target=thread_main, args=(10,)))
# 启动所有线程
for t in threads:
t.start()
if __name__ == '__main__':
num = 4
# 创建4个线程
main(4)
t1 = threading.Thread(target=showthetime, args=())
t1.setDaemon(True)
t1.start()
sys.exit()