简体   繁体   English

无法在Python PyQt4上关闭对话框

[英]Cannot close Dialog on Python PyQt4

My program has a MainWindow which contains my inventory system. 我的程序有一个MainWindow,其中包含我的清单系统。 I have added a Dialog window which pop-ups when I clicked "Add Item". 我添加了一个对话框窗口,当我单击“添加项目”时会弹出该窗口。 I am able to successfully open the Dialog window but I can't seem to close it. 我能够成功打开“对话框”窗口,但似乎无法关闭它。

When the user tries to close the Dialog Window, it will display a messagebox asking if the user really wants to close the window. 当用户尝试关闭对话框窗口时,它将显示一个消息框,询问用户是否真的要关闭该窗口。 Currently, I'm using self.close() . 目前,我正在使用self.close() It just closes the MessageBox I've made to prevent accidental exit and doesn't close the Dialog window unless you end it using the IDE or task manager. 它只是关闭我为防止意外退出而创建的MessageBox,并且除非您使用IDE或任务管理器结束它,否则不会关闭“对话框”窗口。

Here's my code snippets: 这是我的代码段:

Main.py Main.py

class Main(QtGui.QMainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)

        self.db = Database()
        self.model = Model(self)
        self.ui = MainWindow_ui()
        self.ui.setupUi(self)
        self.window = Ui_Dialog()

        self.ui.addItem.clicked.connect(lambda : self.start_Form())

    def start_Form(self):
        window = QtGui.QDialog()
        self.window.setupUi(window)
        self.window.show()

def main():
    app = QtGui.QApplication(sys.argv)
    window = Main()
    window.showMaximized()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

AddItem.py (contains the Dialog Window code) AddItem.py(包含对话框窗口代码)

def getNumber():
    conn = sqlite3.connect('inventory.db')
    c = conn.cursor()
    c.execute('SELECT seq FROM sqlite_sequence')
    itemNumber = c.fetchone()[0]
    return int(itemNumber) + 1

class Ui_Dialog(QtGui.QDialog):
    def __init__(self):
        QtGui.QDialog.__init__(self)
        self.setupUi(self)

    def setupUi(self, Dialog):
        Dialog.setObjectName(_fromUtf8("Dialog"))
        Dialog.resize(413, 382)

        self.buttonBox = QtGui.QDialogButtonBox(Dialog)
        self.buttonBox.setStandardButtons(
            QtGui.QDialogButtonBox.Cancel | QtGui.QDialogButtonBox.Ok | QtGui.QDialogButtonBox.Reset)
        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)

    def retranslateUi(self, Dialog):    
        self.itemCode.setText(str(getNumber()))

    def accept(self):
        row = self.mapper.currentIndex()
        self.mapper.submit()
        self.main.model.insertRow(row)
        self.mapper.setCurrentIndex(row)

        self.close()

    def reject(self):
        ret = QtGui.QMessageBox.question(None, 'Close request', 'Are you sure you want to quit?',
                                         QtGui.QMessageBox.Yes | QtGui.QMessageBox.No)
        if ret == QtGui.QMessageBox.Yes:
            self.close()
        else:
            pass

Your accept() and reject() methods effectively create an infinite loop, because calling close() will, in turn, just call those methods again. 您的accept()reject()方法有效地创建了一个无限循环,因为调用close()将依次再次调用这些方法。 When overriding virtual methods, you should call the base-class implementation using super : 覆盖虚拟方法时,应使用super调用基类实现:

class Ui_Dialog(QtGui.QDialog):
    ...

    def accept(self):
        ...
        super(Ui_Dialog, self).accept()  

    def reject(self):
        ...
        if ret == QtGui.QMessageBox.Yes:
            super(Ui_Dialog, self).reject()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM