Y
Y
Yura Milevsky2021-11-13 18:23:14
Python
Yura Milevsky, 2021-11-13 18:23:14

How to take data from QLineEdit and bind click to button in PyQt5?

I have this code:

The code
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'Yulia.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets
import os
import sys


class Ui_MainWidget(object):
    def setupUi(self, MainWidget):
        MainWidget.setObjectName("MainWidget")
        MainWidget.setEnabled(True)
        MainWidget.resize(391, 500)
        MainWidget.setMinimumSize(QtCore.QSize(0, 0))
        MainWidget.setMaximumSize(QtCore.QSize(10000000, 10000000))
        MainWidget.setAutoFillBackground(False)
        self.label = QtWidgets.QLabel(MainWidget)
        self.label.setGeometry(QtCore.QRect(80, 0, 241, 51))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(36)
        font.setBold(False)
        font.setWeight(50)
        font.setStyleStrategy(QtGui.QFont.PreferDefault)
        self.label.setFont(font)
        self.label.setTextFormat(QtCore.Qt.AutoText)
        self.label.setObjectName("label")
        self.Program1 = QtWidgets.QLineEdit(MainWidget)
        self.Program1.setGeometry(QtCore.QRect(10, 100, 371, 31))
        self.Program1.setText("")
        self.Program1.setObjectName("Program1")
        self.Program1_2 = QtWidgets.QLineEdit(MainWidget)
        self.Program1_2.setGeometry(QtCore.QRect(10, 180, 371, 31))
        self.Program1_2.setText("")
        self.Program1_2.setObjectName("Program1_2")
        self.ProgramN1 = QtWidgets.QLabel(MainWidget)
        self.ProgramN1.setGeometry(QtCore.QRect(110, 60, 171, 41))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(19)
        font.setBold(False)
        font.setWeight(50)
        font.setStyleStrategy(QtGui.QFont.PreferDefault)
        self.ProgramN1.setFont(font)
        self.ProgramN1.setTextFormat(QtCore.Qt.AutoText)
        self.ProgramN1.setObjectName("ProgramN1")
        self.ProgramN2 = QtWidgets.QLabel(MainWidget)
        self.ProgramN2.setGeometry(QtCore.QRect(110, 140, 171, 41))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(19)
        font.setBold(False)
        font.setWeight(50)
        font.setStyleStrategy(QtGui.QFont.PreferDefault)
        self.ProgramN2.setFont(font)
        self.ProgramN2.setTextFormat(QtCore.Qt.AutoText)
        self.ProgramN2.setObjectName("ProgramN2")
        self.pushButton = QtWidgets.QPushButton(MainWidget)
        self.pushButton.setGeometry(QtCore.QRect(20, 390, 351, 41))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(20)
        self.pushButton.setFont(font)
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(MainWidget)
        self.pushButton_2.setGeometry(QtCore.QRect(20, 440, 351, 41))
        font = QtGui.QFont()
        font.setFamily("Times New Roman")
        font.setPointSize(20)
        self.pushButton_2.setFont(font)
        self.pushButton_2.setObjectName("pushButton_2")

        self.retranslateUi(MainWidget)
        QtCore.QMetaObject.connectSlotsByName(MainWidget)

    def retranslateUi(self, MainWidget):
        _translate = QtCore.QCoreApplication.translate
        MainWidget.setWindowTitle(_translate("MainWidget", "Юлька"))
        self.label.setText(_translate("MainWidget", "Юлия 0.2.0"))
        self.ProgramN1.setText(_translate("MainWidget", "Программа №1"))
        self.ProgramN2.setText(_translate("MainWidget", "Программа №2"))
        self.pushButton.setText(_translate("MainWidget", "Запустить программу №1"))
        self.pushButton_2.setText(_translate("MainWidget", "Запустить программу №2"))

    def copy_text(self, number):
        if  number == 1:
            text1 = self.Program1.toPlainText()         # <---   - text()   +++ toPlainText
            print(text + "Всё  ок, нумбер1")

        elif number == 2:
            text1 = self.Program1_2.toPlainText()         # <---   - text()   +++ toPlainText
            print(text + "Всё  ок, нумбер2")

    #pushButton.clicked.connect(copy_text(1))
    #pushButton_2.clicked.connect(copy_text(2))
    self.pushButton.clicked.connect(copy_text(1))
    self.pushButton_2.clicked.connect(copy_text(2))



class Window(QtWidgets.QMainWindow):
    def __init__(self):
        super(Window, self).__init__()
        self.ui = Ui_MainWidget()
        self.ui.setupUi(self)


if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    application = Window()
    application.show()
    sys.exit(app.exec_())

On the
#pushButton.clicked.connect(copy_text(1))
    #pushButton_2.clicked.connect(copy_text(2))
    self.pushButton.clicked.connect(copy_text(1))
    self.pushButton_2.clicked.connect(copy_text(2))

Never mind, these were pathetic attempts to tie the action in any way. I looked at a lot of guides, and here, on the hub, and on the stack offer, none of them helped.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
alexbprofit, 2021-11-13
@alexbprofit

self.pushbutton.pressed.connect

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question