K
K
Kirill Zaporzhin2021-06-30 20:51:36
Python
Kirill Zaporzhin, 2021-06-30 20:51:36

How to get the value of lineEdit when calling a function from a module?

Hello, I'm starting to learn Python and PyQt and I have a question about calling a function from a module and working with PyQt objects. There are 3 files. I need to get value from lineEdit in Second.py function and then call that function in First.py. BUT the value in lineEdit is set inside First.py. lineEdit was originally created in the file design.py
Below is the simplified code:

design.py

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

# Form implementation generated from reading ui file 'design.ui'
#
# Created by: PyQt5 UI code generator 5.13.2
#
# 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(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(230, 190, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(370, 190, 75, 23))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

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

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "PushButton"))


First.py
import sys
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import *
import design
from Second import Hax


class App(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.lineEdit.setText("22")
        self.pushButton.clicked.connect(self.Test)

    def Test(self):
        Hax().calc()


def main():
    App = QtWidgets.QApplication([])
    window = App()
    window.show()
    app.exec_()


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = App()
    window.show()
    sys.exit(app.exec_())


Second.py
from PyQt5 import QtWidgets
import design


class Hax(QtWidgets.QMainWindow, design.Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)

    def calc(self):
        print(self.lineEdit.text())
        print("Текст?")

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
Oleg Andreev, 2021-07-01
@RolingHome

There may be an easier way, but I would do something in between these scripts. For example, enter a string into qLineEdit, return it as text, then save the text into some text document and read this text document from the second script.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question