In PyQt using QLineEdit.setText to add text?

5cfcdf917c0ea009994581.jpeg

Good afternoon, friends!

Write saloner (screen attached).
I searched the Internet, but no results.

There are several buttons, each of them a specific text.
Clicking the contents of each button is added to the QLineEdit ... BUT each following button OUTSHINES his previous text.

You want the text joined to previous.
No idea.

PLEASE TELL ME...

Below is the UI file:

___________________________design.py__________________________________
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'design.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!

from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
 def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
 MainWindow.resize(554, 343)
 self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
 self.scrollArea = QtWidgets.QScrollArea(self.centralwidget)
self.scrollArea.setEnabled(True)
 self.scrollArea.setGeometry(QtCore.QRect(20, 20, 121, 181))
 self.scrollArea.setMinimumSize(QtCore.QSize(121, 121))
self.scrollArea.setMouseTracking(False)
self.scrollArea.setTabletTracking(False)
self.scrollArea.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.scrollArea.setAcceptDrops(False)
self.scrollArea.setToolTip("")
self.scrollArea.setToolTipDuration(-1)
self.scrollArea.setLayoutDirection(QtCore.Qt.LeftToRight)
self.scrollArea.setAutoFillBackground(False)
self.scrollArea.setFrameShape(QtWidgets.QFrame.NoFrame)
self.scrollArea.setFrameShadow(QtWidgets.QFrame.Raised)
self.scrollArea.setLineWidth(1)
self.scrollArea.setMidLineWidth(7)
self.scrollArea.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)
self.scrollArea.setHorizontalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOff)
self.scrollArea.setSizeAdjustPolicy(QtWidgets.QAbstractScrollArea.AdjustIgnored)
self.scrollArea.setWidgetResizable(True)
self.scrollArea.setObjectName("scrollArea")
 self.scrollAreaWidgetContents = QtWidgets.QWidget()
 self.scrollAreaWidgetContents.setGeometry(QtCore.QRect(0, 0, 104, 244))
self.scrollAreaWidgetContents.setObjectName("scrollAreaWidgetContents")
 self.gridLayout = QtWidgets.QGridLayout(self.scrollAreaWidgetContents)
self.gridLayout.setObjectName("gridLayout")
 self.button_1 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.button_1.setEnabled(True)
self.button_1.setMouseTracking(False)
self.button_1.setTabletTracking(False)
self.button_1.setFocusPolicy(QtCore.Qt.StrongFocus)
self.button_1.setContextMenuPolicy(QtCore.Qt.NoContextMenu)
self.button_1.setStyleSheet("")
self.button_1.setInputMethodHints(QtCore.Qt.ImhNone)
self.button_1.setObjectName("button_1")
 self.gridLayout.addWidget(self.button_1, 0, 0, 1, 1)
 self.button_2 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.button_2.setObjectName("button_2")
 self.gridLayout.addWidget(self.button_2, 1, 0, 1, 1)
 self.button_3 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.button_3.setObjectName("button_3")
 self.gridLayout.addWidget(self.button_3, 2, 0, 1, 1)
 self.button_4 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.button_4.setObjectName("button_4")
 self.gridLayout.addWidget(self.button_4, 3, 0, 1, 1)
 self.button_5 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.button_5.setObjectName("button_5")
 self.gridLayout.addWidget(self.button_5, 4, 0, 1, 1)
 self.button_6 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.button_6.setObjectName("button_6")
 self.gridLayout.addWidget(self.button_6, 5, 0, 1, 1)
 self.button_7 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.button_7.setObjectName("button_7")
 self.gridLayout.addWidget(self.button_7, 6, 0, 1, 1)
 self.button_8 = QtWidgets.QPushButton(self.scrollAreaWidgetContents)
self.button_8.setObjectName("button_8")
 self.gridLayout.addWidget(self.button_8, 7, 0, 1, 1)
self.scrollArea.setWidget(self.scrollAreaWidgetContents)
 self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
 self.lineEdit.setGeometry(QtCore.QRect(150, 21, 241, 71))
self.lineEdit.setText("")
self.lineEdit.setEchoMode(QtWidgets.QLineEdit.Normal)
self.lineEdit.setDragEnabled(False)
self.lineEdit.setCursorMoveStyle(QtCore.Qt.LogicalMoveStyle)
self.lineEdit.setObjectName("lineEdit")
 self.splitter = QtWidgets.QSplitter(self.centralwidget)
 self.splitter.setGeometry(QtCore.QRect(150, 100, 221, 101))
self.splitter.setOrientation(QtCore.Qt.Vertical)
self.splitter.setObjectName("splitter")
 self.pushButton = QtWidgets.QPushButton(self.splitter)
self.pushButton.setObjectName("pushButton")
 self.button_copy = QtWidgets.QPushButton(self.splitter)
self.button_copy.setObjectName("button_copy")
 self.butto_clear = QtWidgets.QPushButton(self.splitter)
self.butto_clear.setObjectName("butto_clear")
MainWindow.setCentralWidget(self.centralwidget)

self.retranslateUi(MainWindow)
self.button_copy.clicked.connect(self.lineEdit.copy)
self.butto_clear.clicked.connect(self.lineEdit.clear)
self.pushButton.pressed.connect(self.lineEdit.selectAll)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

 def retranslateUi(self, MainWindow):
 _translate = QtCore.QCoreApplication.translate
 MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
 self.button_1.setToolTip(_translate("MainWindow", "<html><head/><body><p><br/></p></body></html>"))
 self.button_1.setText(_translate("MainWindow", "Button 1"))
 self.button_2.setText(_translate("MainWindow", "Button 2"))
 self.button_3.setText(_translate("MainWindow", "Button 3"))
 self.button_4.setText(_translate("MainWindow", "Button 4"))
 self.button_5.setText(_translate("MainWindow", "Button 5"))
 self.button_6.setText(_translate("MainWindow", "Button 6"))
 self.button_7.setText(_translate("MainWindow", "Button 7"))
 self.button_8.setText(_translate("MainWindow", "Button 8"))
 self.lineEdit.setPlaceholderText(_translate("MainWindow", "Press button..."))
 self.pushButton.setText(_translate("MainWindow", "Select ALL"))
 self.button_copy.setText(_translate("MainWindow", "Copy"))
 self.butto_clear.setText(_translate("MainWindow", "Clear"))


___________________________main.py__________________________________
import sys

from design import * # Import the interface from a file
from PyQt5 import QtCore, QtGui, QtWidgets


class MyWin(QtWidgets.QMainWindow):
 def __init__(self, parent=None):
 QtWidgets.QWidget.__init__(self, parent)
 self.ui = Ui_MainWindow()
self.ui.setupUi(self)

 # Here write the press event for button 1
self.ui.button_1.clicked.connect(self.MyFunction_1)

 # Here write the press event for button 2
self.ui.button_2.clicked.connect(self.MyFunction_2)


# Is empty for the function to execute when the button is pressed 1
 def MyFunction_1(self):
 a = "My Button1 Ok";
self.ui.lineEdit.setText(a)

# Is empty for the function to execute when the button is pressed 1
 def MyFunction_2(self):
 b = "My Ok Button2";
self.ui.lineEdit.setText(b)




if __name__=="__main__":
 app = QtWidgets.QApplication(sys.argv)
 myapp = MyWin()
myapp.show()
 sys.exit(app.exec_())
March 23rd 20 at 19:44
2 answers
March 23rd 20 at 19:46
Solution
I have realized that you need to add text in a LineEdit:
setText () Changes the text in full
there is an option to do
self variable:
class MyWin(QtWidgets.QMainWindow):
 def __init__(self, parent=None):
 self.text = "" # self variable
 QtWidgets.QWidget.__init__(self, parent)
 self.ui = Ui_MainWindow()
self.ui.setupUi(self)

 # Here write the press event for button 1
self.ui.button_1.clicked.connect(self.MyFunction_1)

 # Here write the press event for button 2
 self.ui.button_2.clicked.connect(self.MyFunction_2)

And when you call the function to add to the self variable text and setText(self.text)
Code:
def MyFunction_1(self):
 self.text += "My Button1 Ok"
self.ui.lineEdit.setText(self.text)
def MyFunction_2(self):
 self.text += "My Button2 Ok"
 self.ui.lineEdit.setText(self.text)

Clearing to zero the self variable.

And in the code
def MyFunction_2(self):
 b = "My Ok Button2";
 self.ui.lineEdit.setText(b)

Optional add ;
March 23rd 20 at 19:48
Solution
First, take something that already exists in the element, and then add something new:
self.ui.lineEdit.setText(self.ui.lineEdit.text() + some_text)

some_text can be a variable or a literal in quotes.

Find more questions by tags QtPython