0

I have two Forms. I need to get text from (lineedit) in Ui_Mainwindow1 and make label in Ui_MainWindow2 with that text. and this two forms in two seprate files

Can anyone please explain how can update label in (Ui_MainWindow2) with the text in (lineedit) in (Ui_Mainwindow1) when i press on pushbutton in (Ui_MainWindow1)

> class Ui_MainWindow1(object):
>     def setupUi(self, MainWindow):
>         MainWindow.setObjectName("MainWindow")
>         self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
>         self.lineEdit.setGeometry(QtCore.QRect(130, 70, 181, 21))
>         self.lineEdit.setObjectName("lineEdit")
>         self.pushButton = QtWidgets.QPushButton(self.centralwidget)
>         self.pushButton.setGeometry(QtCore.QRect(140, 100, 161, 23))
>         self.pushButton.setObjectName("pushButton")
>         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", "update"))
> 
> 
> if __name__ == "__main__":
>     import sys
>     app = QtWidgets.QApplication(sys.argv)
>     MainWindow = QtWidgets.QMainWindow()
>     ui = Ui_MainWindow1()
>     ui.setupUi(MainWindow)
>     MainWindow.show()
>     sys.exit(app.exec_())



class Ui_MainWindow2(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(410, 165)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(160, 40, 111, 21))
        self.label.setText("")
        self.label.setObjectName("label")
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

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


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow2()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())
maboseteit
  • 11
  • 2
  • 3
    There is a new tool cold google, you basically type stuff like "pyqt examples" and it gives you what you want. – user1767754 Dec 23 '17 at 20:17
  • 2
    SO is not a code writing service. Please do some research first. Your question is also lacking sufficient detail, so your intention is not clear. Research would be a good place to put your effort. – ShpielMeister Dec 23 '17 at 20:31

1 Answers1

0

amm What you can do is maintain a link between the two windows with help of some file or a database.Where you can connect pushButton to a slot which will update the file with the text in the lineedit and you can add a threading function(can use QtCore.QThread) in 2nd form that picks the text from the file and set as label's text.Hope it might solve your problem,but at the same I think the answer will receive several down votes as well.

Alternatively, if you are familiar with QSQLDriver you could do something like updating database with the text of lineedit on signal emitting from text change and you could use the same database to read the text as well from the database as soon as sql database change occurs.I think for this second option the link below might help

https://stackoverflow.com/a/22919225/7698247

Hope it helps

I tried to implement the first option and was successful in doing that.I create two files g1.py and g2.py and a text file (filename.txt)

g1.py

import sys
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon


class Example(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

#        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('web.png'))        
        self.lineedit=QtWidgets.QLineEdit()
        self.vlayout=QtWidgets.QVBoxLayout()
        self.vlayout.addWidget(self.lineedit)
        self.lineedit.textChanged.connect(self.updatemy)
        self.setLayout(self.vlayout)
        self.show()

    def updatemy(self,string):
        x=open("filename.txt","w")
        x.write(string)
        x.close()


if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

g2.py

import sys
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon
from PyQt5 import QtCore
import threading



class Example(QtWidgets.QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):
        self.val=""
#        self.setGeometry(300, 300, 300, 220)
        self.setWindowTitle('Icon')
        self.setWindowIcon(QIcon('web.png'))        
        self.label=QtWidgets.QLabel()
        self.label.setText("hello")

        self.vlayout=QtWidgets.QVBoxLayout()
        self.vlayout.addWidget(self.label)
        self.setLayout(self.vlayout)
        self.launch_thread()
        self.show()

    def get_content(self):
        while 1:
            x=open("filename.txt","r")
            self.val=x.read()
            x.close()
            self.label.setText(self.val)




    def launch_thread(self):
        myThread=threading.Thread(target=self.get_content)
        myThread.start()                

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

To test it I ran commands below:

 def f1():
    os.system("python g1.py")
 def f2():
    os.system("python g2.py")
 a=threading.Thread(target=f1)
 b=threading.Thread(target=f2) 
 a.start()
 b.start()   
Nimish Bansal
  • 1,719
  • 4
  • 20
  • 37