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



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

代码如下:

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

请帮忙!

每次显示小部件时都调用初始化代码。将所有这些移到它所属的__init__方法中,它就可以正常工作了。

将旁边的内容移到init方法中。我不能确切地说为什么运行init代码会破坏连接。但不知何故,它确实做到了。也许其他人可以填补这个细节。

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

相关内容

  • 没有找到相关文章

最新更新