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

2 投票
1 回答
1449 浏览
提问于 2025-04-18 07:01

接着我上一个问题,我又遇到麻烦了。我想从子部件更新父部件的内容。代码第一次运行的时候没问题,但在关闭再打开表单部件后,父部件的内容就不更新了。

以下是代码。

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

你每次显示这个小部件的时候都在调用初始化代码。把这些代码都放到__init__方法里,这样就能正常工作了。

把除了这个以外的所有东西都放进初始化方法里。我不能确切说为什么多次运行初始化代码会导致连接出问题,但确实是这样。也许其他人能解释得更清楚。

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

撰写回答