使用pyside从表单2更新表单1

时间:2013-09-06 15:35:09

标签: python qt pyside

我正在尝试使用以下代码从表单2中的LineEdit值更新表单1中的Label。它没有给我任何错误,但值不会更新。我有什么问题吗?

main.py:

import First,Second
import sys
from PySide import QtGui,QtCore

class ControlWidget(QtGui.QWidget):

    def __init__(self, parent=None):
        super(ControlWidget, self).__init__(parent)
        self.ui =  First.Ui_Form()
        self.ui.setupUi(self)

    def setValues(self,value):
        print value
        print "HEre"
        self.ui.label.setText(value)
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    mySW = ControlWidget()
    mySW.show()
    sys.exit(app.exec_())

First.py:

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

# Form implementation generated from reading ui file 'First.ui'
#
# Created: Wed Nov 01 16:37:02 2000
#      by: pyside-uic 0.2.15 running on PySide 1.2.1
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui
import Second
class Ui_Form(QtGui.QWidget):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(160, 92)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(40, 20, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.label = QtGui.QLabel(Form)
        self.label.setGeometry(QtCore.QRect(50, 60, 46, 13))
        self.label.setObjectName("label")
        QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL("clicked()"),self.showseconddiag)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)

    def showseconddiag(self):

        self.Diag = QtGui.QDialog(self)
        self.secondinstance = Second.Ui_Form(self)
        self.secondinstance.setupUi(self.Diag)
        self.Diag.show()

    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("Form", "TextLabel", None, QtGui.QApplication.UnicodeUTF8))

Second.py:

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

# Form implementation generated from reading ui file 'Second.ui'
#
# Created: Wed Nov 01 16:37:16 2000
#      by: pyside-uic 0.2.15 running on PySide 1.2.1
#
# WARNING! All changes made in this file will be lost!

from PySide import QtCore, QtGui
import First,main

class Ui_Form(QtGui.QWidget):
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(191, 94)
        self.pushButton = QtGui.QPushButton(Form)
        self.pushButton.setGeometry(QtCore.QRect(40, 20, 75, 23))
        self.pushButton.setObjectName("pushButton")
        self.lineEdit = QtGui.QLineEdit(Form)
        self.lineEdit.setGeometry(QtCore.QRect(30, 50, 113, 20))
        self.lineEdit.setObjectName("lineEdit")
        QtCore.QObject.connect(self.pushButton,QtCore.SIGNAL("clicked()"),self.returnvalue)
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
    def returnvalue(self):
        self.finstance = main.ControlWidget(self)
        self.finstance.setValues(self.lineEdit.text())
    def retranslateUi(self, Form):
        Form.setWindowTitle(QtGui.QApplication.translate("Form", "Form", None, QtGui.QApplication.UnicodeUTF8))
        self.pushButton.setText(QtGui.QApplication.translate("Form", "PushButton", None, QtGui.QApplication.UnicodeUTF8))

1 个答案:

答案 0 :(得分:1)

在这一行

    self.finstance = main.ControlWidget(self)

您正在创建ControlWidget的新实例。您需要在setValues的现有实例上调用ControlWidget

您可以将第一个表单的引用传递给setupUi或第二个表单的构造函数:

#Second.py
class Ui_Form(QtGui.QWidget):
    def setupUi(self, Form, FirstForm):
         self.FirstForm = FirstForm
         ...

# First.py

# We need to pass the instance to the setupUi call of the 2nd form

# First, we need to save parent form, as we will pass that to the
# second form (we can't just pass self, as this is Ui_Form, and not the 
# ControlWidget instance
def setupUi(self, Form):
    self.ParentForm = Form
    ...

self.secondinstance.setupUi(self.Diag, self.ParentForm)

然后你可以在该对象上使用setValues:

def returnvalue(self):
    self.FirstForm.setValues(self.lineEdit.text());