PyQt使父GUI等到子GUI关闭

时间:2016-02-18 11:50:50

标签: python python-3.x pyqt pyqt4

我是pyqt的新手。我正在尝试在父GUI中单击按钮时调用子GUI。在此过程中,父GUI必须等待用户在选择一些输入后关闭子GUI。但是没有发生这种情况,父GUI确实执行了下一行,之后调用了子GUI。下面是我从父GUI向子GUI传递参数的代码。子GUI将根据OK / Cancel按钮单击

返回值

代码:

import sys
from PyQt4 import QtGui,QtCore,Qt
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Child(QtGui.QWidget):
  def __init__(self,switches=None):
    super(Child,self).__init__()
    self.swwidget = QtGui.QWidget()
    self.swlayout = QtGui.QGridLayout()
    switches = ['abc1','def1']
    switches.sort()
    self.switches = switches

  def switchesUI(self):
    self.swwidget.setWindowModality(QtCore.Qt.ApplicationModal)
    self.swl   = len(self.switches)
    self.sw    = {}
    self.addsw = []
    print ("I am in switchesUI")
    #Add the switches to layout dynamically
    for i in range(self.swl):
        self.sw[i] = QtGui.QCheckBox(self.switches[i])
        self.swlayout.addWidget(self.sw[i],i,0)
    self.swbuttonbox = QtGui.QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel);
    self.swbuttonbox.setOrientation(QtCore.Qt.Horizontal)
    self.swlayout.addWidget(self.swbuttonbox)
    self.swwidget.setWindowTitle('Switches')
    self.swwidget.setLayout(self.swlayout)
    self.swwidget.show()
    self.connect(self.swbuttonbox,QtCore.SIGNAL("accepted()"),self.swaccept)
    self.connect(self.swbuttonbox,QtCore.SIGNAL("rejected()"),self.swreject)

  def swaccept(self):
    for i in range(self.swl):
      if self.sw[i].isChecked():
         self.addsw.append(self.switches[i])
    self.swwidget.close()
    return self.addsw

  def swreject(self):
    self.swwidget.close()
    return None

class Parent(QtGui.QWidget):
  def __init__(self):
    super(Parent,self).__init__()
    QtGui.QWidget.__init__(self)
    self.button = QtGui.QPushButton('Test', self)
    self.layout = QtGui.QVBoxLayout(self)
    self.layout.addWidget(self.button)
    self.assw = ['Test1','Test2']
    self.CH = Child(self.assw)
    self.connect(self.button,SIGNAL("clicked()"),self.popup)
    print ("Child GUI closed")

  def popup(self):
    self.CH.switchesUI()

def main():
  app = QtGui.QApplication(sys.argv)
  form = Parent()
  form.show()
  sys.exit(app.exec_())

if __name__ == '__main__':
  main()

单击“测试”按钮后,将弹出一个子GUI。在子GUI关闭之前,我不希望打印“Child GUI Closed”语句。 有人可以建议我如何实现这个功能吗?

1 个答案:

答案 0 :(得分:0)

您必须处理closeEvent以在窗口想要关闭时执行操作,因为您的Child类继承自QWidget,这意味着它本身就是QWidget需要使用self.swwidget

创建另一个
import sys
from PyQt4 import QtGui,QtCore,Qt
from PyQt4.QtCore import *
from PyQt4.QtGui import *

class Child(QtGui.QWidget):
  def __init__(self,switches=None):
    super(Child,self).__init__()
    # self.swwidget = QtGui.QWidget() # you don't need to do this you can add all the properties to self
    self.swlayout = QtGui.QGridLayout()
    switches = ['abc1','def1']
    switches.sort()
    self.switches = switches

  def switchesUI(self):
    self.setWindowModality(QtCore.Qt.ApplicationModal)
    self.swl   = len(self.switches)
    self.sw    = {}
    self.addsw = []
    print ("I am in switchesUI")
    #Add the switches to layout dynamically
    for i in range(self.swl):
        self.sw[i] = QtGui.QCheckBox(self.switches[i])
        self.swlayout.addWidget(self.sw[i],i,0)
    self.swbuttonbox = QtGui.QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel);
    self.swbuttonbox.setOrientation(QtCore.Qt.Horizontal)
    self.swlayout.addWidget(self.swbuttonbox)
    self.setWindowTitle('Switches')
    self.setLayout(self.swlayout)
    self.show()
    self.connect(self.swbuttonbox,QtCore.SIGNAL("accepted()"),self.swaccept)
    self.connect(self.swbuttonbox,QtCore.SIGNAL("rejected()"),self.swreject)

  def swaccept(self):
    for i in range(self.swl):
      if self.sw[i].isChecked():
         self.addsw.append(self.switches[i])
    self.close()
    return self.addsw

  def swreject(self):
    self.close()
    return None

  def closeEvent(self, event):
      print ("Child GUI closed")

class Parent(QtGui.QWidget):
  def __init__(self):
    super(Parent,self).__init__()
    QtGui.QWidget.__init__(self)
    self.button = QtGui.QPushButton('Test', self)
    self.layout = QtGui.QVBoxLayout(self)
    self.layout.addWidget(self.button)
    self.assw = ['Test1','Test2']
    self.CH = Child(self.assw)
    self.connect(self.button,SIGNAL("clicked()"),self.popup)

  def popup(self):
    self.CH.switchesUI()


def main():
  app = QtGui.QApplication(sys.argv)
  form = Parent()
  form.show()
  sys.exit(app.exec_())

if __name__ == '__main__':
  main()