带有链接脚本的其他类的PyQt4 setText动态

时间:2018-07-26 15:39:27

标签: python multithreading pyqt pyqt4 rfid

一切都代替了此不便之处。 我试图设置文本,但是没有用。 每次新扫描都应显示从标签扫描的新文本,但与来自RFID脚本的全局变量的文本相同。

from PyQt4 import QtCore, QtGui
from ui_mainwindow import Ui_MainWindow
import threading
import RPi.GPIO as GPIO
import MFRC522
import signal
import time

class MainWindow(QtGui.QMainWindow):
    state1, state2, state3, state4 = range(4)
    stateChanged = QtCore.pyqtSignal(int)


    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.stateChanged.connect(self.onChangeState)
        threading.Thread(target=self.reading, daemon=True).start()

    def onChangeState(self, state):
        MIFAREReader = MFRC522.MFRC522()
        if state == MainWindow.state1:

            self.ui.label_4.setText(MIFAREReader.tagId) ###-->text from RFID script
            self.ui.label_2.setText(MIFAREReader.tagName) ###-->text from RFID script
            self.ui.label_3.setText(MIFAREReader.tagDetails) ###-->text from RFID script

            self.ui.label_3.show()
            self.ui.label_2.show()
            self.ui.label_4.show()
            self.ui.groupBox.setStyleSheet("background: white;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;")

        elif state == MainWindow.state2:
            self.ui.label_3.hide()
            self.ui.label_2.hide()
            self.ui.label_4.hide()
            self.ui.groupBox.setStyleSheet("background: white url(scan.png) no-repeat center;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;")

        elif state == MainWindow.state3:
            self.ui.groupBox.setStyleSheet("background: white url(accsd.png) no-repeat center;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;")

        elif state == MainWindow.state4:
            self.ui.groupBox.setStyleSheet("background: white url(scan.png) no-repeat center;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;")


    def reading(self):
        ### Event Functions ###
        continue_reading = True
        # Hook the SIGINT
        #signal.signal(signal.SIGINT, end_read)
        # Create an object of the class MFRC522
        MIFAREReader = MFRC522.MFRC522()
        # This loop keeps checking for chips. If one is near it will get the UID and authenticate
        while continue_reading:
            # Scan for cards    
            (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
            # Get the UID of the card
            (status,uid) = MIFAREReader.MFRC522_Anticoll()

            # If we have the UID, continue
            if status == MIFAREReader.MI_OK:
                # This is the default key for authentication
                key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
                # Select the scanned tag
                MIFAREReader.MFRC522_SelectTag(uid)
                # Authenticate
                status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
                # Check if authenticated
                if status == MIFAREReader.MI_OK:

                    MIFAREReader.MFRC522_Read(8)
                    self.stateChanged.emit(MainWindow.state1)
                    time.sleep(3)
                    MIFAREReader.MFRC522_StopCrypto1()
                    self.stateChanged.emit(MainWindow.state2)

                else:
                    self.stateChanged.emit(MainWindow.state3)
                    time.sleep(2)
                    self.stateChanged.emit(MainWindow.state4)


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())

这是我要从RFID脚本在GUI中打印的文本的代码。

class MFRC522(): 
  #Strings for GUI
  tagId = "txt"
  tagName = "txt"
  tagDetails = "txt"
      #########
      #########
      #########
      #########
  def MFRC522_Read(self, blockAddr):
    self.tagId = "Product ID: "
    self.tagName = "Product Name: "
    self.tagDetails = "Product Details: "

    recvData = []
    recvData.append(self.PICC_READ)
    recvData.append(blockAddr)
    pOut = self.CalulateCRC(recvData)
    recvData.append(pOut[0])
    recvData.append(pOut[1])
    (status, backData, backLen) = self.MFRC522_ToCard(self.PCD_TRANSCEIVE, recvData)
    if not(status == self.MI_OK):
      message = "Error while reading!"
##      print (message)
    i = 0

    backDataText = ""
    if len(backData) == 16:
        for x in range(0, len(backData)):
            backDataText = backDataText + chr(int(backData[x]))

    for iA in range(len(self.indexMark)):
        numbersFound = re.search(r'\d+', backDataText)
        DataTextNumeric = numbersFound.group()
        DataTextNumeric = int(DataTextNumeric)
        if DataTextNumeric == self.indexMark[iA]:
            message = "Product Code: %s \n Product Name: %s \n Details: %s" % (backDataText, self.productsName[iA], self.productsDetails[iA])
            Id = DataTextNumeric
            Name = self.productsName[iA]
            Details = self.productsDetails[iA]
##            print (message)

            break
    self.tagId += str(Id)
    self.tagName += Name
    self.tagDetails += Details
    print (self.tagId) ###-->"Product ID: ####" this prints in the console   
                          every time I scan a new thing, so the code works

代码很好,但是我无法理解它是如何工作的。每次读取新标签时,GUI上的消息都是RFID脚本中全局变量的值未更改。我没有太多经验,对此我感到抱歉。

1 个答案:

答案 0 :(得分:0)

onChangeState()中包含以下行:

MIFAREReader = MFRC522.MFRC522()

您正在创建另一个对象,该对象不同于reading()中创建的对象,因此将不具有RFID信息,这使我认为您必须加强OOP概念。

如您上一个问题所述,应使用以下信号发送数据:

from PyQt4 import QtCore, QtGui
from ui_mainwindow import Ui_MainWindow
import threading
import RPi.GPIO as GPIO
import MFRC522
import signal
import time

class MainWindow(QtGui.QMainWindow):
    state1, state2, state3, state4 = range(4)
    stateChanged = QtCore.pyqtSignal(int)
    infoChanged = QtCore.pyqtSignal(str, str, str)

    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.infoChanged.connect(self.onInfoChanged)
        self.stateChanged.connect(self.onChangeState)
        threading.Thread(target=self.reading, daemon=True).start()

    def onInfoChanged(self, text1, text2, text3):
        self.ui.label_4.setText(text1) ###-->text from RFID script
        self.ui.label_2.setText(text2) ###-->text from RFID script
        self.ui.label_3.setText(text3) ###-->text from RFID script

    def onChangeState(self, state):
        if state == MainWindow.state1:
            self.ui.label_3.show()
            self.ui.label_2.show()
            self.ui.label_4.show()
            self.ui.groupBox.setStyleSheet("background: white;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;")

        elif state == MainWindow.state2:
            self.ui.label_3.hide()
            self.ui.label_2.hide()
            self.ui.label_4.hide()
            self.ui.groupBox.setStyleSheet("background: white url(scan.png) no-repeat center;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;")

        elif state == MainWindow.state3:
            self.ui.groupBox.setStyleSheet("background: white url(accsd.png) no-repeat center;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;")

        elif state == MainWindow.state4:
            self.ui.groupBox.setStyleSheet("background: white url(scan.png) no-repeat center;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;")


    def reading(self):
        ### Event Functions ###
        continue_reading = True
        # Hook the SIGINT
        #signal.signal(signal.SIGINT, end_read)
        # Create an object of the class MFRC522
        MIFAREReader = MFRC522.MFRC522()
        # This loop keeps checking for chips. If one is near it will get the UID and authenticate
        while continue_reading:
            # Scan for cards    
            (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
            # Get the UID of the card
            (status,uid) = MIFAREReader.MFRC522_Anticoll()

            # If we have the UID, continue
            if status == MIFAREReader.MI_OK:
                # This is the default key for authentication
                key = [0xFF,0xFF,0xFF,0xFF,0xFF,0xFF]
                # Select the scanned tag
                MIFAREReader.MFRC522_SelectTag(uid)
                # Authenticate
                status = MIFAREReader.MFRC522_Auth(MIFAREReader.PICC_AUTHENT1A, 8, key, uid)
                # Check if authenticated
                if status == MIFAREReader.MI_OK:

                    MIFAREReader.MFRC522_Read(8)
                    self.stateChanged.emit(MainWindow.state1)
                    # send data via signal
                    self.infoChanged.emit(MIFAREReader.tagId, MIFAREReader.tagName, MIFAREReader.tagDetails)
                    time.sleep(3)
                    MIFAREReader.MFRC522_StopCrypto1()
                    self.stateChanged.emit(MainWindow.state2)

                else:
                    self.stateChanged.emit(MainWindow.state3)
                    time.sleep(2)
                    self.stateChanged.emit(MainWindow.state4)


if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    w = MainWindow()
    w.show()
    sys.exit(app.exec_())