多线程问题" QBasicTimer只能用于以QThread开头的线程"

时间:2017-06-02 15:31:01

标签: python multithreading opencv pyqt5

我正在使用Python 3.4开发一个程序,我使用OpenCV来检测带有实时视频的符号,并且我还使用PyQt5来开发GUI。这是我的代码:

# coding=utf-8
import kivy
kivy.require("1.9.2")

from PyQt5 import QtCore, QtWidgets, uic
from picamera.array import PiRGBArray
from picamera import PiCamera
import numpy as np
import cv2
import time
import RPi.GPIO as GPIO
import sys
import threading

qtCreatorFile = "mainwindow.ui"
Ui_MainWindow, QtBaseClass = uic.loadUiType(qtCreatorFile)

class MyApp(QtWidgets.QMainWindow, Ui_MainWindow, threading.Thread):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
        self.stackedWidget.setCurrentIndex(0)
        print("Index0")
        case=0
        self.iniciar_button.clicked.connect(self.teste,case)

    def teste(self, case):

        if case==0:
            self.stackedWidget.setCurrentIndex(1)
            t= threading.Thread(target=self.processo)
            t.start()
            print("case0")
        if case==1:
            print("case1")
            self.stackedWidget.setCurrentIndex(2)
            i=0
            while i==0:
                if self.iniciar2.clicked():
                    self.stackedWidget.setCurrentIndex(1)
                    d= threading.Thread(target=self.processo)
                    d.start()
                    i=1

##    @asyncio.coroutine    
    def processo(self):
        global garrafasA
        global garrafasR
        f=1
        while f==0:
            GPIO.setwarnings(False)
            GPIO.setmode(GPIO.BOARD)
            entradaIR = 11
            GPIO.setup(entradaIR,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)
            entrada = GPIO.input(entradaIR)
            if entrada==1:
                f=1

         (do something more here but nothing important)

        while flag1 == 1 and saida == 1 and x == 1: # Activar tapete de ACEITAÇÃO
            print ("Aceitacao")
            garrafasA += 1
            time.sleep(1.5)
            saida = GPIO.input(saidaIR)
            pwm1.start(DC)
            GPIO.output(motor1A,GPIO.HIGH)
            GPIO.output(motor1B,GPIO.LOW)
            GPIO.output(motor1E,GPIO.HIGH)
            saida = GPIO.input(saidaIR)
            self.nGarrafasA.display(garrafasA)
            if saida == 0:
                break

        entrada = GPIO.input(entradaIR)
        while flag2 == 1 and entrada == 0: # Inverter tapete de ACEITAÇÃO
            print ("inverter")
            if flag3 == 0:
                garrafasR += 1
                flag3 = 1
            time.sleep(1.5)
            pwm1.start(DC)
            GPIO.output(motor1A,GPIO.LOW)
            GPIO.output(motor1B,GPIO.HIGH)
            GPIO.output(motor1E,GPIO.HIGH)
            entrada = GPIO.input(entradaIR)
            self.nGarrafasR.display(garrafasR)

        entrada = GPIO.input(entradaIR)
        while flag2 == 1 and entrada == 1: # Parar tapete de entrada na devolução              
            print ("Entrada",entrada)
            print ("Parar tapete")
            GPIO.output(motor1E,GPIO.LOW)
            pwm1.stop()
            flag2=0
            case=1
            self.teste(case)
            return


if __name__ == "__main__":
    garrafasA = 0
    garrafasR = 0
    app = QtWidgets.QApplication(sys.argv)
    window = MyApp()
    window.show()
    sys.exit(app.exec_())

我创建了一个新线程,可以让GUI运行并同时在函数" processo"上进行符号检测,但是一旦这个函数结束,它就会返回到"情形1"关于功能" teste"等待按钮" iniciar2。 这是打印" case1"之后的问题。并将页面更改为索引2我收到消息" QBasicTimer只能用于以QThread"开头的线程。

任何人都知道我做错了什么?有人可以帮帮我吗?我不久前处理过这个错误,我无法确定问题所在。

提前谢谢。

0 个答案:

没有答案