从另一个小部件更新pyqt小部件内容

时间:2014-05-19 17:29:12

标签: python forms widget pyqt updates

从我的上一个问题开始,我又被卡住了。我正在尝试从子窗口小部件更新父窗口小部件的内容。代码似乎第一次工作,但在关闭并重新打开表单小部件后,它不会更新父小部件。

以下是代码。

from PyQt4 import QtGui, QtCore
from functools import partial
import sys


class MainWidget(QtGui.QWidget):
    def __init__(self):
       super(MainWidget, self).__init__()

       self.main_widget()

    def main_widget(self):
        self.form = Form()
        self.simple = Simple()
        grid = QtGui.QGridLayout()


        self.last_input_label = QtGui.QLabel("")
        grid.addWidget(self.last_input_label, 1, 0, 3, 1)

        show_form_button = QtGui.QPushButton("Show Form")
        show_form_button.clicked.connect(partial(self.form.show_form, self.last_input_label))
        grid.addWidget(show_form_button, 0, 0)

        show_simple_button = QtGui.QPushButton("Show Simple")
        show_simple_button.clicked.connect(self.simple.show_simple)
        grid.addWidget(show_simple_button, 0, 1)

        another_button = QtGui.QPushButton("Print Hello")
        another_button.clicked.connect(partial(print, "Hello"))
        grid.addWidget(another_button, 0, 2)

        self.setLayout(grid)
        self.setWindowTitle("Main Widget")
        self.show()

    def closeEvent(self, QCloseEvent):
        QtGui.QApplication.closeAllWindows()


class Form(QtGui.QWidget):
    def __init__(self):
        print("form initialized")
        super(Form, self).__init__()

    def show_form(self, last_input_label):
        print("form called")
        grid = QtGui.QGridLayout()

        self.last_input_label = last_input_label

        label = QtGui.QLabel("Name")
        grid.addWidget(label, 0, 0)


        self.line_edit = QtGui.QLineEdit()
        grid.addWidget(self.line_edit, 0, 1)

        self.submit_button = QtGui.QPushButton("Submit")
        self.submit_button.clicked.connect(self.print_form_data)
        grid.addWidget(self.submit_button, 1, 1)

        self.setLayout(grid)
        self.setGeometry(250, 300, 250, 150)
        self.setWindowTitle("Form Widget")
        self.show()

    def get_form_data(self):
        form_data = {
            "name": self.line_edit.text()
        }
        return form_data

    def print_form_data(self):
        self.x = self.get_form_data()
        for item in self.x:
            print(item + ": " + self.x[item])
            self.last_input_label.setText(self.x[item])

        return


class Simple(QtGui.QDialog):
    def __init__(self):
        print("simple initialized")
        super(Simple, self).__init__()

    def show_simple(self):
        print("simple called")
        self.setGeometry(300, 250, 250, 150)
        self.setWindowTitle("Simple Widget")
        self.show()

def main():
    app = QtGui.QApplication(sys.argv)
    main_widget = MainWidget()
    sys.exit(app.exec_())


if __name__ == "__main__":
    main()

请帮助!

1 个答案:

答案 0 :(得分:1)

每次显示窗口小部件时都会调用初始化代码。将所有内容移动到它所属的__init__方法中,一切正常。

将此处的所有内容移动到init方法中。我不能确切地说为什么运行init代码会破坏连接。但它确实如此。也许其他人可以填写这个细节。

def show_form(self, last_input_label):
    print("form called")
    self.last_input_label = last_input_label
    self.show()