Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在PyQT中检索转储到stdin中的字符串?_Python_Qt_Pyqt_Barcode_Keypress - Fatal编程技术网

Python 如何在PyQT中检索转储到stdin中的字符串?

Python 如何在PyQT中检索转储到stdin中的字符串?,python,qt,pyqt,barcode,keypress,Python,Qt,Pyqt,Barcode,Keypress,我打开了一个对话框,等待从条形码扫描仪输入条形码。 如果我把键盘的焦点放在LineEdit小部件上,条形码就会简单地插入到字段中。但我不想有这样的设置。我只需要一个对话框来等待代码到达,当它收到代码时,返回主窗口 我试着制作一个计时器,每秒检查stdin中的输入,但没有成功 def handleTimeout(self): inp = sys.stdin.readline() print('You pressed {}'.format(str(inp))) 我没有深入研究,因为

我打开了一个对话框,等待从条形码扫描仪输入条形码。 如果我把键盘的焦点放在
LineEdit
小部件上,条形码就会简单地插入到字段中。但我不想有这样的设置。我只需要一个对话框来等待代码到达,当它收到代码时,返回主窗口

我试着制作一个计时器,每秒检查
stdin
中的输入,但没有成功

def handleTimeout(self):
    inp = sys.stdin.readline()
    print('You pressed {}'.format(str(inp)))
我没有深入研究,因为我认为无论如何都应该有更好、更有效的方法

如何继续?
我想读取从扫描仪接收到的13字节条形码


编辑

def keyPressEvent(self, event):
         if type(event) == QtGui.QKeyEvent:
             stt = chr(event.key())
             if (not stt.isdigit()): return
             self.barcode += chr(event.key())
             print(self.barcode)
             if (len(self.barcode) == 13): self.close()
             event.accept()
         else:
             event.ignore()

它实际上是有效的。但我可以看到很多事情可能会出错。就像用户可以在扫描开始前按一两个键一样。我如何纠正这些错误?我可以只从扫描仪而不是任何键盘捕获事件吗?

尝试使用事件的方法来测试按键是否来自应用程序外部。

扫描仪肯定会将其数据放入内存缓冲区,不是吗?扫描仪仅生成键盘事件?我刚刚尝试过,它返回按键和条形码扫描仪数据的
false
。看起来条形码扫描器只是模仿键盘输入(或者可能只是这个特定型号)。无论如何,我目前的解决方案是:当一个有效的按键事件发生时,一个计时器会启动200毫秒。如果它在该时间范围内接收到剩余的数字,则它接受输入。否则它将重置stt。一个键盘通常不允许在这么短的时间内按这么多键。@ShashwatBlack。看起来很合理。我在其他地方也读到过,可以将扫描仪配置为在实际条形码之前发出前缀签名。谢谢。。我会调查的。