我创建2个Windows testWidget和win2。首先在testWidget中,单击“开始”菜单中的“输入测量数据”,然后在win2中的L1(m)的LineEdit中输入数字,然后按“ Analyze”并将其显示在testWidget中。我做到了,但是它将再次弹出testWidget。怎么解决呢?

谢谢!

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_()

最佳答案

您需要设置一个用于访问的全局变量。

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

并且,让子窗口将值传递给GUI。
def getre(self):
    text_LT = self.line1.text()
    GUI.linetest.setText(text_LT)

08-16 18:04