The essence of the question in the title. When received using .text or .toPlainText, and trying to remake it into an integer using int (variable), the UI crashes. Thanks in advance for your reply.

    2 answers 2

    QLineEdit has only .text() , and .toPlainText() is for QTextEdit and QPlainTextEdit .

    In addition, if an int(...) is passed, something other than a numeric value, there will be an exception that, if not caught, the application will break:

     from PyQt5.Qt import QLineEdit, QApplication app = QApplication([]) line_edit = QLineEdit('123') value = line_edit.text() print(value) # "123" print(int(value)) # 123 

    Ps. To catch an exception in Qt slots, add code:

     def log_uncaught_exceptions(ex_cls, ex, tb): text = '{}: {}:\n'.format(ex_cls.__name__, ex) import traceback text += ''.join(traceback.format_tb(tb)) print(text) QMessageBox.critical(None, 'Error', text) quit() import sys sys.excepthook = log_uncaught_exceptions 

    Pps. why do you need QLineEdit ? Maybe it is better to use QSpinBox ? QSpinBox has a .value() method that will return an int


    PPPS. I will add an example of checking the string value when converting to a number:

     from PyQt5.Qt import * class Window(QWidget): def __init__(self): super().__init__() self.line_edit = QLineEdit('123') self.button_check = QPushButton('Проверить число!') self.button_check.clicked.connect(self._on_check) layout = QVBoxLayout() layout.addWidget(self.line_edit) layout.addWidget(self.button_check) self.setLayout(layout) def _on_check(self): text = self.line_edit.text() try: value = int(text) QMessageBox.information(self, 'Информация', 'Введено валидное число: "{}"'.format(value)) except ValueError: QMessageBox.warning(self, 'Внимание', 'Введено невалидное число: "{}"'.format(text)) if __name__ == '__main__': app = QApplication([]) mw = Window() mw.show() app.exec() 
    • Even if a numeric value is entered, it still breaks down :). - AgeofCreations
    • What mistake? And add to the question the minimum code example with your error - gil9red
    • But thanks, it worked. - AgeofCreations
    • It just crashed when I tried to translate str to int - AgeofCreations
    • @AgeofCreations, in such cases, make a check and notification, for example try: value = int(value_str) except ValueError as e: QMessageBox.critical(None, 'Error', "Введено невалидное число '{}'".format(value_str)) - gil9red

    I also assume that it is better to use QSpinBox.

    But if you still need to convert 'str' to 'int', then this can be done as follows:

     import re >>> string = " Это какая-то строка 123 возможно с цифрами 4567" >>> int("0"+"".join(list(map(str, re.findall(r'\d+', string))))) 1234567 >>> >>> string = "12345" >>> int("0"+"".join(list(map(str, re.findall(r'\d+', string))))) 12345 >>> >>> string = "" >>> int("0"+"".join(list(map(str, re.findall(r'\d+', string))))) 0 >>> >>> string = "БлаБлаБла" >>> int("0"+"".join(list(map(str, re.findall(r'\d+', string))))) 0 >>> >>> string = " " >>> int("0"+"".join(list(map(str, re.findall(r'\d+', string))))) 0