2014-05-19 41 views
1

從我上一個問題開始,我再一次陷入困境。我試圖從子部件更新父部件的內容。代碼似乎第一次工作,但在關閉並重新打開表單小部件後,它不會更新父部件。從另一個部件更新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() 

請幫忙!

回答

1

您每次顯示小部件時都會調用初始化代碼。將所有這些移動到__init__方法所屬的位置,並且它都可以工作。

將此處的所有內容移動到init方法中。我不能確切地說明爲什麼更多地運行init代碼會破壞連接。但不知怎麼的。也許別人可以填寫這些細節。

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

Thanx幫助@ M4rtini它的工作。還發現只有將self.line_edit = QtGui.QLineEdit()移入__init__方法才能完成這項工作。但爲了佈局的緣故,我將所有初始化語句移至__init__方法,現在一切正常。 – Haroon