我对线程/ GUI很新,我尝试创建的是一个对话框,要求用户扫描条形码(通过串口),然后根据该对象填充数据条形码。
扫描和填充部分没问题。我的问题是,当我打开这个对话框时,我开始从串口读取以查看是否有任何扫描。这会冻结GUI并且不允许用户取消。所以经过一些研究后,看起来我想做一些线程,但我无法弄明白。
以下是我所拥有的:
...
self.edit_location = EditLocation(self)
self.edit_location.show()
self.edit_location.wait_for_scan()
class EditLocation(QDialog):
def __init__(self, parent=None):
QDialog.__init__(self, parent)
self.ui = ui_edit_location.Ui_EditLocation()
self.ui.setupUi(self)
self.parent = parent
def wait_for_scan(self):
thread = ScanThread()
thread.start()
def receivedBarcode(self, barcode):
print "recieved", barcode
class ScanThread(QThread):
def __init__(self):
QThread.__init__(self)
def __del__(self):
self.wait()
def run(self):
barcode = scan_barcode()
self.emit(SIGNAL('receivedBarcode'), barcode)
并在我的扫描文件中......(使用pyBarcode)
s = serial.Serial(scanner_port)
def scan_barcode():
return s.readline()
GUI仍会冻结,直到扫描条形码。任何帮助将不胜感激!