簡體   English   中英

帶有鏈接腳本的其他類的PyQt4 setText動態

[英]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_())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM