Reputation: 7
I'm writing a code, where I want to Scan a barcode and ask for a specific input. Right now I did it with a "save-button", where I have to ask manually if the input is correct.
But I want to do that automatically when I wrote an input.
My problem:
When I write an input without the "save-button" the program doesn't take the changed input of the textbox. And if I use a while-loop, my program is crashing and it doesn't show the UI.
I want to read it with the variable "self.inp_sn" and ask if there is the right length and only digits. But without success. Maybe there is a way to "delay" the class or make a loop without crashing the system, but sadly I couldn't find any good solution.
The Code:
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(300, 100)
self.UI()
def UI(self):
self.set_sn()
self.click_button()
self.show()
def set_sn(self):
print(1234)
self.sn_Textbox = QLineEdit(self)
self.sn_Textbox.move(85, 35)
self.sn_Textbox.resize(130,20)
self.sn_Textbox.setPlaceholderText('Seriennummer')
def checkstatus(self):
self.state = 0
self.inp_sn = self.sn_Textbox.text()
if len(self.inp_sn) == 7 and self.inp_sn.isdigit():
print("Your Text: ", self.sn_Textbox.text())
self.sn_Textbox.setFocusPolicy(Qt.StrongFocus)
self.state = 1
else:
print(self.inp_sn)
print("Wrong input")
def click_button(self):
self.save_sn = QPushButton("Save",self)
self.save_sn.move(85, 55)
self.save_sn.clicked.connect(self.checkstatus)
def main():
App = QApplication(sys.argv)
window = Window()
sys.exit(App.exec())
if __name__ == "__main__":
main()
Upvotes: 1
Views: 135
Reputation: 243897
By default barcodes add an endline(\n
) so in your case you just have to use the editingFinished
signal:
class Window(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Seriennummer")
self.setFixedSize(300, 100)
self.UI()
def UI(self):
self.set_sn()
self.show()
def set_sn(self):
print(1234)
self.sn_Textbox = QLineEdit(self)
self.sn_Textbox.move(85, 35)
self.sn_Textbox.resize(130, 20)
self.sn_Textbox.setPlaceholderText("Seriennummer")
self.sn_Textbox.editingFinished.connect(self.checkstatus)
def checkstatus(self):
print(self.sn_Textbox.text())
def click_button(self):
self.save_sn = QPushButton("Save", self)
self.save_sn.move(85, 55)
self.save_sn.clicked.connect(self.checkstatus)
Upvotes: 2