R
R
R1nG1n02020-05-08 19:49:08
Python
R1nG1n0, 2020-05-08 19:49:08

How to write to variables and output some text in PyQt5?

Hello. There is such code: https://repl.it/repls/PrettyGoodDiscussion
Tell me how to write text from QLineEdit to a variable? And how to organize output in QListWidget?
I'm sorry for the link, because. there are 2 files.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander, 2020-05-08
@sanya84

import sys
from PyQt5.QtWidgets import *


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()

sys.excepthook = log_uncaught_exceptions


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()

        self.setWindowTitle('Передача значения из QLineEdit в QListWidget')
        self.setGeometry(300, 300, 500, 400)

        vbox = QVBoxLayout(self)

        self.line_edit = QLineEdit()
        self.list_widget = QListWidget()

        self.button = QPushButton('Запустить')
        self.button.clicked.connect(self.add_item)

        vbox.addWidget(self.line_edit)
        vbox.addWidget(self.list_widget)
        vbox.addWidget(self.button)

    def add_item(self):
        self.list_widget.addItem(self.line_edit.text()) #QLineEdit метод метод text возвращает текст
                                                        # По QListWidget читайте документацию
        print(self.line_edit.text())

if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = MainWindow()
    main.show()
    sys.exit(app.exec_())

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question