简体   繁体   中英

External functions in main file PyQt4

I tried really hard to make this code to work. I tried with Qtimer, proccesEvents and I didn't have the results I wanted. The GUI starts, so something works, but there are no changes, so the method reading() doesn't work.

I searched a lot on stackoverflow to find some help, but I couldn't find. Perhaps I am not capable.

This is the code:

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

class MainWindow(QtGui.QWidget):
    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)

        self.ui = Ui_MainWindow()

        self.ui.setupUi(self)

    def reading(self):
        self.ui.processEvents()
        ### 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.ui.label_3.show()
                    self.ui.label_2.show()
                    self.ui.label_4.show()
                    self.ui.groupBox.setStyleSheet(_fromUtf8("background: white;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;"))

                    time.sleep(5)
                    MIFAREReader.MFRC522_StopCrypto1()
                    self.ui.label_3.hide()
                    self.ui.label_2.hide()
                    self.ui.label_4.hide()
                    self.ui.groupBox.setStyleSheet(_fromUtf8("background: white url(scan.png) no-repeat center;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;"))
                else:
                    self.ui.groupBox.setStyleSheet(_fromUtf8("background: white url(accsd.png) no-repeat center;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;"))
                    time.sleep(3)
                    self.ui.groupBox.setStyleSheet(_fromUtf8("background: white url(scan.png) no-repeat center;\n"
                    "border-style: solid;\n"
                    "border-width: 1px;\n"
                    "border-radius: 20px;"))

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    MainWindow = QtGui.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

You never call the function reading() ? So why should it go in there?

I'm not sure what you want to achieve, but you could try:

def __init__(self, parent=None):
    QtGui.QWidget.__init__(self, parent)

    self.ui = Ui_MainWindow()

    self.ui.setupUi(self)

    self.reading() ####################

But that will only call the function once. When do you want reading() to execute?

The task of reading data is blocking so it should not run on the thread of the GUI but on another thread, on the other hand you should not change the GUI from another thread but communicate the change through signals as I show below:

import MFRC522
# import signal
import time

from PyQt4 import QtCore, QtGui
from ui_mainwindow import Ui_MainWindow
import threading

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):
        if state == MainWindow.state1:
            self.ui.label_3.show()
            self.ui.label_2.show()
            self.ui.label_4.show()
            self.ui.groupBox.setStyleSheet(_fromUtf8("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(_fromUtf8("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(_fromUtf8("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(_fromUtf8("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(5)
                    MIFAREReader.MFRC522_StopCrypto1()
                    self.stateChanged.emit(MainWindow.state2)

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM