Python 主文件PyQt4中的外部函数
我真的很努力地使这个代码工作。我尝试了Qtimer、proccesEvents,但没有得到我想要的结果。GUI启动,所以有些东西可以工作,但是没有变化,所以方法读取不起作用 我在stackoverflow上搜索了很多以寻求帮助,但找不到。 也许我没有能力 代码如下:Python 主文件PyQt4中的外部函数,python,pyqt4,Python,Pyqt4,我真的很努力地使这个代码工作。我尝试了Qtimer、proccesEvents,但没有得到我想要的结果。GUI启动,所以有些东西可以工作,但是没有变化,所以方法读取不起作用 我在stackoverflow上搜索了很多以寻求帮助,但找不到。 也许我没有能力 代码如下: import RPi.GPIO as GPIO import MFRC522 import signal import time from PyQt4 import QtCore, QtGui from ui_mainwindow
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_())
你从不调用函数reading?那为什么要放进去呢 我不确定您想要实现什么,但您可以尝试:
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.reading() ####################
但这只会调用函数一次。何时执行读取?读取数据的任务被阻塞,因此它不应该在GUI的线程上运行,而应该在另一个线程上运行,另一方面,您不应该从另一个线程更改GUI,而是通过信号传达更改,如下所示:
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_())
结论很好,但不起作用。这不是问题。问题是什么?问题是读取方法中的while循环将阻塞gui。您需要将该代码放在一个单独的工作线程中,并将信号发送回gui,以便它可以在必要时执行更新。有几十篇关于这个场景的帖子,所以只要寻找,你就会发现。