pyqt barcode scanner lineEdit

南笙酒味 提交于 2019-12-02 02:35:36
Nimish Bansal
from PyQt5 import QtWidgets,QtCore
import sys
import os
class window(QtWidgets.QMainWindow):
    def __init__(self):
        super(window,self).__init__()
        self.mylineEdit = QtWidgets.QLineEdit()
        self.mylineEdit2 = QtWidgets.QLineEdit()
        self.startNew=1
        #initialise to empty string on start up
        self.mylineEdit.setText(' ')


        #barcode scans here and then a returnPressed is registered

        #connect to a function
        self.mylineEdit.returnPressed.connect(self.set_sample_name) #here is where I want to delete the previous entry without backspacing by hand
        self.mylineEdit.textChanged.connect(self.delete_previous)

        centwid=QtWidgets.QWidget()
        lay=QtWidgets.QVBoxLayout()
        lay.addWidget(self.mylineEdit)
        lay.addWidget(self.mylineEdit2)
        centwid.setLayout(lay)
        self.setCentralWidget(centwid)
        self.show()

        #set the sample name variable
    def set_sample_name(self):
        self.sample_name = self.mylineEdit.text()
        print(self.sample_name)
        self.startNew=1
    def delete_previous(self,text):
        if self.startNew:
            self.mylineEdit.setText(text[-1])
            self.startNew=0
app=QtWidgets.QApplication(sys.argv)
ex=window()
sys.exit(app.exec_())

As soon as return pressed signal is executed you can change the flag self.startNew=1 which will ensure whenever the text will change it will check the flag and remove the complete string as soon as new barcode is entered. I have done in PyQt5 but the concept will remain the same. The functionality is achieved in self.myLineEdit.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!