我非常努力地使此代码起作用。我尝试使用Qtimer,proccesEvents,但没有想要的结果。 GUI启动,因此可以正常工作,但是没有任何更改,因此read()方法不起作用。
我在stackoverflow上进行了大量搜索以寻求帮助,但找不到。 也许我没有能力。
这是代码:
<p>Selected bot: {{selectedBot.name}}</p>
<form #form="ngForm">
<div class="btn-group" style="width: 100%">
<div style="width: 100%">
<input type="text" id="add" name="bot_id" ngModel placeholder="select a bot" [value]="selectedBot && selectedBot.name || ''">
<i class="fa fa-angle-down center-vertical-absolute"></i>
</div>
<ul class="dropdown-menu" role="menu" style="width: 100%; max-height: 30vh; overflow-y: scroll">
<li role="menuitem" *ngFor="let bot of botList" (click)="botSelected(bot)"><a
class="dropdown-item" [ngClass]="{'dropdown-item-active': selectedBot && selectedBot._id===bot._id}">{{bot.name}}</a>
</li>
</ul>
</div>
</form>
答案 0 :(得分:1)
您从不调用函数reading()
吗?那么为什么要去那里呢?
我不确定您要实现什么,但是您可以尝试:
def __init__(self, parent=None):
QtGui.QWidget.__init__(self, parent)
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.reading() ####################
但是那只会调用一次函数。您何时希望reading()
执行?
答案 1 :(得分:0)
读取数据的任务正在阻塞,因此它不应在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_())