Python 用PyQt读取条形码:为什么类要读取两次输入?
上周我在PyQt5和读取条形码方面遇到了问题。问题解决了。现在我有另一个问题。我可以读取条形码,但我的输入总是两次。所以在我结束程序之前,程序进入“check.status”类两次,然后退出程序 有没有办法计算输入,或者只是等待输入正确?也许可以尝试一下 这是我当前的编码:Python 用PyQt读取条形码:为什么类要读取两次输入?,python,pyqt5,Python,Pyqt5,上周我在PyQt5和读取条形码方面遇到了问题。问题解决了。现在我有另一个问题。我可以读取条形码,但我的输入总是两次。所以在我结束程序之前,程序进入“check.status”类两次,然后退出程序 有没有办法计算输入,或者只是等待输入正确?也许可以尝试一下 这是我当前的编码: from PyQt5 import QtCore, QtGui, QtWidgets from PyQt5.QtGui import * from PyQt5.QtCore import * from PyQt5.QtW
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
# from PyQt5.QtWidgets import QMessageBox, QWidget
import sys
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Seriennummer")
self.setFixedSize(250, 100)
self.UI()
def UI(self):
self.set_sn()
self.show()
def set_sn(self):
self.sn_Textbox = QLineEdit(self)
self.sn_Textbox.move(85, 35)
self.sn_Textbox.resize(130, 20)
self.sn_Textbox.setMaxLength(7)
self.sn_Textbox.setPlaceholderText("Seriennummer")
self.sn_Textbox.setFocusPolicy(Qt.StrongFocus)
self.onlyInt = QIntValidator()
self.sn_Textbox.setValidator(self.onlyInt)
self.sn_Textbox.editingFinished.connect(self.checkstatus)
def checkstatus(self):
self.inp_sn = self.sn_Textbox.text()
print("Check")
if len(self.inp_sn) == 7 and self.inp_sn.isdigit():
print("Your Text: ", self.sn_Textbox.text())
self.close()
else:
print(self.inp_sn)
print("Wrong input")
self.error_msg()
def error_msg(self):
msg_error = QMessageBox()
msg_error.setWindowTitle("Error message")
msg_error.setText("Wrong input: Überprüfe Seriennummer")
msg_error.setIcon(QMessageBox.Warning)
x = msg_error.exec()
def main():
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
if __name__ == "__main__":
main()
我放置了printf()-函数来查看输出,因此您也可以看到问题。正如所指出的:
当按下回车键或回车键或行编辑失去焦点时,会发出此信号。请注意,如果在“编辑”行上设置了validator()或inputMask(),并按下enter/return,则只有在输入跟随inputMask()且validator()返回QValidator::Acceptable时,才会发出editingFinished()信号 editingFinished信号的发出有几个原因,在您的情况下,它会发出两次:第一次是由端点(
\n
)发出的,第二次是当QLineEdit失去焦点时发出的。因此,一个可能的解决方案是检查小部件是否具有焦点(使用hasFocus()
)。但在这种情况下,最好使用returnPressed(返回按下)信号:
导入系统
从PyQt5.QtCore导入pyqtSignal,Qt
从PyQt5.QtGui导入QIntValidator
从PyQt5.QtWidgets导入QApplication、QLineEdit、QMessageBox、QWidget
类窗口(QWidget):
定义初始化(自):
super()。\uuuu init\uuuuu()
self.setWindowTitle(“Seriennummer”)
自我设置固定大小(250100)
self.UI()
def用户界面(自我):
self.set_sn()
def设置序列号(自):
self.barcode\u le=QLineEdit(self)
自身条形码设置几何(85、35、130、20)
self.barcode\u le.returnPressed.connect(self.handle\u returnPressed)
self.barcode_le.setMaxLength(7)
self.barcode_le.setplaceholder文本(“序列号摘要”)
self.barcode\u le.setFocusPolicy(Qt.StrongFocus)
验证器=QIntValidator()
self.barcode_le.setValidator(验证器)
def手柄返回按下(自):
text=self.barcode\u le.text()
如果len(text)==7且text.isdigit()为:
打印(“您的文本:”,文本)
self.close()
其他:
打印(文本)
打印(“错误输入”)
self.error_msg()
def错误消息(自身):
msg_error=QMessageBox()
msg_error.setWindowTitle(“错误消息”)
msg_error.setText(“错误输入:Überprüfe Seriennummer”)
msg_error.setIcon(QMessageBox.Warning)
x=msg_error.exec()
def main():
App=QApplication(sys.argv)
window=window()
window.show()
sys.exit(App.exec())
如果名称=“\uuuuu main\uuuuuuuu”:
main()
工作正常。对于初学者来说,这也是很好的安排。我的问题是:您编辑的代码与“BarcodeLineEdit”类的最大区别是什么,您可以从中获取事件和状态。更紧凑?@PYlearning09这是一样的,只是我意识到已经有一个信号检测到了终点线,所以没有必要做同样的工作