PyQt:父窗口不等待子窗口关闭。

时间:2017-03-23 22:44:27

标签: python-2.7 pyqt pyqt4 parent

PyQt:父窗口不等到子窗口关闭。参考下面共享的代码,我的欢迎类对象应该等到first_time类对象完全执行完毕,而是继续并在first_time对象完成执行之前自行关闭它。

代码:

from PyQt4.QtGui import *
from PyQt4.QtCore import *
from PySide.QtCore import QSettings
import sys
from PyQt4 import uic
#importing first configuration class
import configure as config_first
#loading initial settings
settings=QSettings('settings.ini',QSettings.IniFormat)

#loading the ui screens
form_class=uic.loadUiType("screens/firstscreen.ui")[0]


class welcome(QDialog,form_class):
    #this signal is emitted when first configuration is done and ready to go
    done_and_go_to_use = pyqtSignal()
    def __init__(self):
        super(welcome, self).__init__()
        self.setupUi(self)
        self.done_and_go_to_use.connect(self.close)
        self.ready_btn.clicked.connect(self.ready)

    def ready(self):

        if_configured = settings.value('isConfigured', False)
        if not if_configured :
            first_time=config_first.configureFirst(self)
            first_time.show()
        self.close()

app = QApplication(sys.argv)
p = welcome()
p.show()
app.exec_()

下面是configure.py

的代码
from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sqlite3
import csv

from PySide.QtCore import QSettings

from PyQt4 import uic
#loading initial settings
settings=QSettings('settings.ini',QSettings.IniFormat)
#loading ui screens
form_class=uic.loadUiType("screens/config_first.ui")[0]
#database connecting
con = sqlite3.connect("local.db")
cur = con.cursor()
cur.execute("CREATE TABLE if not exists marks (student_id int,student_name varchar(200));")

class configureFirst(QDialog,form_class):
    done_configuring=pyqtSignal()
    try_again=pyqtSignal()
    def __init__(self,parent=None):
        super(configureFirst, self).__init__(parent)
        self.setupUi(self)
        self.ok_btn.clicked.connect(self.ok_clicked)
        self.cancel_btn.clicked.connect(self.cancel_clicked)
        self.try_again.connect(self.ok_clicked)
        self.done_configuring.connect(self.cancel_clicked)
        self.show()




    def ok_clicked(self):
        file_select=QFileDialog.getOpenFileName(self,"open file","/")
        if file_select:
            with open(file_select, 'rb') as f:
                reader = csv.reader(f)
                ed = list(reader)


            for row in ed:
                    if "name" not in row or "id" not in row:
                        cur.execute("Insert into marks Values (?,?);",(row[0],row[1]))
                        con.commit()
            settings.setValue("isConfigured",True)

            self.done_configuring.emit()
        else:
            #if recurssion is used the no of time it has to close increases and leads to integration problems
            self.try_again.emit()

    def cancel_clicked(self):
        if_configured=settings.value("isConfigured")
        if if_configured:
            self.close()
        else:
            QMessageBox.critical(self,"PerfAnalyser","You Need to Configure For PerfAnalyser To Work")

    def closeEvent(self,event):
        #this method is triggered when 'X' is clicked i.e close button is clicked at the upper right corner
        if_configured = settings.value("isConfigured")
        if if_configured:
            event.accept()
        else:
            QMessageBox.critical(self, "PerfAnalyser", "You Need to Configure For PerfAnalyser To Work")
            event.ignore()

先谢谢你的帮助......

1 个答案:

答案 0 :(得分:0)

我会尽力帮忙,因为我发现很少有人看过你的帖子。我很久以前就已经发生了这种情况,所以我需要提醒,但我无法让你的代码运行,我也尝试重新创建你的ui文件和屏幕目录,但我没有成功。但是,可能以下内容仍然有用。

在我的工作代码中,每当我需要创建一个子窗口时,我都会从主窗口的模块中执行以下子窗口:

dlg = SubWindowModuleName.StartSub()
dlg.exec_()

这将执行子窗口并等待它关闭。然后,在子窗口模块(上面代码中的SubWindowModuleName,为您配置“)中,我这样做了:

class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(982, 521)
        ... # here I build the window (I noticed that you import UI files which is a much better way of doing this)

...

class StartSub(QtGui.QDialog, Ui_Dialog):
    def __init__(self,parent=None):
        QtGui.QDialog.__init__(self,parent)
        self.setupUi(self)