1

我正在尝试使用以下代码从表格 2 中 LineEdit 的值更新表格 1 中的标签。它没有给我任何错误,但值只是没有得到更新。有什么我做错了吗?

主要.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_())

首先.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))

第二个.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))
4

1 回答 1

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());
于 2013-09-06T16:21:39.223 回答