from PyQt5.QtWidgets import QMainWindow, QApplication, QLineEdit, QPushButton from PyQt5.QtGui import QIntValidator class MainWindow(QMainWindow): def __init__(self): super().__init__() self.input_field = QLineEdit(self) self.input_field.move(50, 50) self.input_field.setPlaceholderText("Enter an integer between 5 and 100") self.validate_button = QPushButton("Validate input", self) self.validate_button.move(50, 100) # Set the valid range for the input field validator = QIntValidator(5, 100, self) self.input_field.setValidator(validator) # Connect the validate_button to a function that checks the input value self.validate_button.clicked.connect(self.check_input) def check_input(self): # Get the input value from the QLineEdit widget input_value = self.input_field.text() if input_value: print("Input is valid") else: print("Please enter a valid integer") if __name__ == "__main__": app = QApplication([]) window = MainWindow() window.show() app.exec_()
from PyQt5.QtWidgets import QLineEdit from PyQt5.QtGui import QIntValidator, QDoubleValidator def create_input_field(): input_field = QLineEdit() input_field.setValidator(QIntValidator(0, 100)) # Set the valid range for the input field return input_field def create_decimal_field(): decimal_field = QLineEdit() decimal_field.setValidator(QDoubleValidator(0.0, 1.0, 2)) # Set the valid range for the input field return decimal_fieldThis example creates two functions, create_input_field and create_decimal_field, that return QLineEdit widgets. The setRange function is used to set the valid ranges for the input fields.