radiosan radiosan - 2 months ago 18
Python Question

PyQt - Pass number variable between windows

I create 2 windows testWidget and win2. First in testWidget, I click "Input maesure data " in Start menu, then I want to input number in LineEdit of L1(m) in win2, press "Analyze" and shows it in the testWidget. I did it, but it will pop up testWidget again. How to solve it?

Thanks!

import sys
from PyQt4 import QtGui,QtCore

class testWidget(QtGui.QMainWindow):

def __init__(self):

super(testWidget, self).__init__()

self.setGeometry(25,150,1200,700)
self.setWindowTitle('test')

extractAction_1 = QtGui.QAction('&Input maesure data',self)
extractAction_1.triggered.connect( self.newWindow)

self.linetest = QtGui.QLineEdit()
self.linetest.setText("0.0")

layoutV = QtGui.QVBoxLayout()

layoutV.addWidget(self.linetest)
widget = QtGui.QWidget()
widget.setLayout(layoutV)
self.setCentralWidget(widget)


mainMenu = self.menuBar()
fileMenu = mainMenu.addMenu('&Start')
fileMenu.addAction(extractAction_1)

def newWindow(self):

self.myOtherWindow = win2()
self.myOtherWindow.show()

def showtex(self,text_LT):

self.linetest.clear()
self.linetest.setText(text_LT)

class win2(QtGui.QMainWindow):

def __init__(self):

QtGui.QMainWindow.__init__(self)

self.setWindowTitle("Set parameters")
self.setGeometry(150,300,300,200)


b2 = QtGui.QPushButton("Analyze")
b2.clicked.connect(self.getre)

layoutV = QtGui.QVBoxLayout()
layoutH = QtGui.QHBoxLayout()

lab1 = QtGui.QLabel("L1(m):")
self.line1 = QtGui.QLineEdit()
self.line1.setText("50")

lab2 = QtGui.QLabel("L2(m):")
self.line2 = QtGui.QLineEdit()
self.line2.setText("0.0")

self.line3test = QtGui.QLineEdit()


layoutH.addWidget(lab1)
layoutH.addWidget(self.line1)

layoutH.addWidget(lab2)
layoutH.addWidget(self.line2)
layoutV.addLayout(layoutH)

layoutV.addWidget(b2)
layoutV.addWidget(self.line3test)

self.widget = QtGui.QWidget()
self.widget.setLayout(layoutH)
self.widget.setLayout(layoutV)

self.setCentralWidget(self.widget)

self.winma=testWidget()


def getre(self):

text_LT = self.line1.text()


self.winma.showtex(text_LT)
self.winma.show()

if __name__ == '__main__':

app = QtGui.QApplication(sys.argv)
GUI = testWidget()
GUI.show()
app.exec_()

Answer

You need to set a global variable for acessing.

GUI = None
if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    GUI = testWidget()
    GUI.show()
    app.exec_()

And, let the subwindow pass value to the GUI.

def getre(self):
    text_LT = self.line1.text()
    GUI.linetest.setText(text_LT)