Python和PyQt:在退出确认框callEvent()时,它被调用两次

2024-10-01 07:49:27 发布

您现在位置:Python中文网/ 问答频道 /正文

我是Python和PyQt的新手。我试图在关闭主窗口之前管理closeEvent to ask,但这只适用于“X”按钮。从为询问用户而创建的QMEssageBox中,callEvent()被调用了两次。在

这是准则的相关部分:

    self.ui.actionChiudi.triggered.connect(self.close)

def closeEvent(self, event):
    #check presence of data in the table
    if self.csvViewer.rowCount() > 0:
        print event # only to analyze the caller
        #show a warning
        Error = QtGui.QMessageBox()
        Error.setIcon(QtGui.QMessageBox.Question)
        Error.setWindowTitle('ATTENZIONE !!')
        Error.setInformativeText(u"Sei sicuro di voler uscire?")
        Error.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
        ret = Error.exec_()
        if ret == QtGui.QMessageBox.Ok:
            event.accept()
        else:
            event.ignore()
    else:
        #close directly
        event.accept()

“actionChiudi”是主要男士中的男士用品。 据我所知,当使用'X'按钮时,函数close()只从mainwindow对象直接调用一次,然后关闭我的应用程序。 当使用menù项时,函数创建新对象“QMessageBox”,然后为此对象调用一次“closeEvent()”,然后为mainwindow对象调用相同的函数。如果这是对的,我不知道该怎么办。 提前感谢您的帮助!在


Tags: theto对象函数selfeventcloseif
1条回答
网友
1楼 · 发布于 2024-10-01 07:49:27

感谢您关于如何构造一个好的代码示例的提示。对不起,我是新来的,所以我没有成功地树立一个好榜样。但是,与此同时,我在思考这个问题,最后,我用一个小技巧解决了这个问题。

我添加了一个全局变量,如果您正在关闭主窗口,那么在其他调用closeEvent()时避免调用测试过程。 我试图在主类外部创建一个用于生成QMessageBox的类,然后重写该对象的closeEvent(),但没有起作用。我找到的唯一方法就是全局变量。程序继续调用两次closeEvent(一次用于QMessageBox,一次用于主窗口),但现在,第二次调用ignore来调用测试。这是个把戏,不优雅,但对我有用。

现在的代码是:

#!/usr/bin/env python
#-*- coding:utf-8 -*-

import sys
import os
import datetime
import inspect
import csv
import codecs
import pymssql
from PyQt4 import QtGui, QtCore, Qt
import mainwindow
#import _winreg only on Microsoft platforms
import platform
winos = True
if os.name == 'nt' and platform.system() == 'Windows':
    import _winreg
    #other stuff needed under Windows
    import _mssql
    import decimal
    import uuid
    winos = True
else:
    winos = False

#other global variables
liccode = False
closemain = False


class MainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QDialog.__init__(self, parent)
        super(MainWindow, self).__init__(parent)
        self.ui = mainwindow.Ui_MainWindow()
        self.ui.setupUi(self)
        # some stuff and widgets to visualize in the main window

        #connettors to menù functions; leave only the closing one
        self.ui.actionChiudi.triggered.connect(self.close)

    #override of closeEvent - there I think, it's better to work to separate
    #closeEvent for mainwindow from general closeEvent. But how?
    def closeEvent(self, event):
        global closemain
        #make evaluation test to decide how to work. Close directly or no?
        if self.csvViewer.rowCount() > 0 and not closemain:
            print event
            #show a warning
            Error = QtGui.QMessageBox()
            Error.setIcon(QtGui.QMessageBox.Question)
            Error.setWindowTitle('WARNING !!')
            Error.setInformativeText(u"Are you sure to leave?")
            Error.setStandardButtons(QtGui.QMessageBox.Ok | QtGui.QMessageBox.Cancel)
            ret = Error.exec_()
            if ret == QtGui.QMessageBox.Ok:
                closemain = True
                event.accept()
            else:
                event.ignore()
        else:
            #close directly
            event.accept()
  #start application and create main
  app = QtGui.QApplication(sys.argv)
  my_mainWindow = MainWindow()
  my_mainWindow.show()
  sys.exit(app.exec_())

在任何情况下,任何建议都是可以接受的,以便编写更好的代码。谢谢大家!

相关问题 更多 >