S
S
studprogrammist2018-02-10 05:17:41
Python
studprogrammist, 2018-02-10 05:17:41

PyQt write to lineEdit on button click?

# -*- coding: utf-8 -*-

from PyQt5 import QtWidgets, uic
import sys

class MyWindow(QtWidgets.QWidget):
    def __init__(self, parent = None):
        QtWidgets.QWidget.__init__(self, parent)
        Form, Base = uic.loadUiType("window.ui")
        self.ui = Form()
        self.ui.setupUi(self)
        self.ui.lineEdit.setText("Text")  # Пример №1
        self.ui.btnQuit.clicked.connect(self.ui.lineEdit.setText("Text"))  # Пример №2


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

Can you please tell me why example #1 works and example #2 doesn't?
I would be happy to link to suitable material for study :)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
bbkmzzzz, 2018-02-12
@bbkmzzzz

It is possible like this:
or bind to a method

# -*- coding: utf-8 -*-

from PyQt5 import QtWidgets, uic
import sys

class MyWindow(QtWidgets.QWidget):
    def __init__(self, parent = None):
        QtWidgets.QWidget.__init__(self, parent)
        Form, Base = uic.loadUiType("window.ui")
        self.ui = Form()
        self.ui.setupUi(self)
        self.ui.lineEdit.setText("Text")  # Пример №1

        self.ui.btnQuit.clicked.connect(self.setmytext))  # Пример №2 Внимательно со скобками, нам нужно
                                                                                   #передать метод, но не результат его выполнения!

    def setmytext(self):
        self.ui.lineEdit.setText("Text")

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

A
arama92, 2018-05-14
@arama92

https://www.tutorialspoint.com/pyqt/index.htm
cliced.connect(function)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question