簡體   English   中英

從另一個小部件更新pyqt小部件內容

[英]Updating pyqt widget content from another widget

從我的最后一個問題繼續前進,我再次陷入困境。 我正在嘗試從子小部件更新父小部件的內容。 該代碼似乎第一次起作用,但是在關閉並重新打開表單窗口小部件之后,它不會更新父窗口小部件。

以下是代碼。

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方法。 我無法確切地說出為什么再運行初始化代碼會破壞連接。 但是不知何故。 也許其他人可以填寫該細節。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM