来自其他 class 的 PyQt4 setText 动态与 link 脚本

PyQt4 setText dynamic from other class with a link script

一切正常,而不是这种不便。 我试图设置文本,但它不起作用。 每次新扫描都应该显示从标签扫描的新文本,但它是来自 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 脚本中的全局变量值未更改。我没有那么多经验,很抱歉。

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_())