【问题标题】:PyQT CloseEvent function: Main window disappearsPyQT CloseEvent 函数:主窗口消失
【发布时间】:2019-02-06 04:25:11
【问题描述】:

我是 PyQT 的新手。我正在将我的一个应用程序从 tkinter 转移到 PyQT。我想要的是以下场景:当用户单击 X 按钮时,会在主屏幕仍打开时出现询问用户是否确定关闭的消息框。但是,在我的代码中,当单击 X 按钮时,主屏幕首先消失并出现消息框。我该如何解决这个“订单问题” - 正如我所说的那样?我的代码如下:

(在 tkinter 中,这很容易使用代码 root.protocol("WM_DELETE_WINDOW", on_closing) 和包含 messagebox.askokcancel("Quit", "Do you want to quit Chit-Chat?") 命令的 on_closure 函数。但是,我在 PyQT 中无法完全弄清楚。)

app = QApplication(sys.argv)

v_box = QVBoxLayout()

window = QWidget()
label = QLabel("Hello World")

v_box.addWidget(label)

def closeEvent():
    msg_box = QMessageBox()
choice = QMessageBox.question(msg_box, "Quit", "Do you want to quit chit chat?", QMessageBox.Yes | QMessageBox.No)
    if choice == QMessageBox.Yes:
        print("The program was shut down.")
        sys.exit()
    else:
        pass

app.aboutToQuit.connect(closeEvent)
window.setLayout(v_box)
window.show()
sys.exit(app.exec())

【问题讨论】:

    标签: python user-interface pyqt


    【解决方案1】:

    试试看:

    import sys
    from PyQt5.QtGui     import *
    from PyQt5.QtWidgets import *
    from PyQt5.QtCore    import *
    
    class Window(QWidget):
        def __init__(self, parent=None):
            super().__init__(parent)
    
            label = QLabel("Hello World")
    
            v_box = QVBoxLayout()
            v_box.addWidget(label)
            self.setLayout(v_box)
    
        def closeEvent(self, event):
            choice = QMessageBox.question(
                self,  
                "Quit", 
                "Do you want to quit chit chat?", 
                QMessageBox.Yes | QMessageBox.No)
    
            if choice == QMessageBox.Yes:
                print("The program was shut down.")
                event.accept()
            else:
                event.ignore()
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        qt_app = Window()
        qt_app.show()
        sys.exit(app.exec_())
    

    【讨论】:

    • 当我转换成函数范式时这不起作用。我想我错过了什么。
    【解决方案2】:

    您可以选择是否接受该事件。

    def closeEvent(self, event):
        if self.popup_question():
            print("The program was shut down.")
            event.accept()
        else:
            print("not exiting")
            event.ignore()
    
    def popup_question(self):
        """Generate a popup that requests if you want to do something or not."""
        msgbox = QtWidgets.QMessageBox()
        msgbox.setWindowTitle("Whatever title you want to add.")
        msgbox.setIcon(QtWidgets.QMessageBox.Warning)
        msgbox.setText("Do you want to quit chit chat?")
        botonyes = QtWidgets.QPushButton("Yes")
        msgbox.addButton(botonyes, QtWidgets.QMessageBox.YesRole)
        botonno = QtWidgets.QPushButton("No")
        msgbox.addButton(botonno, QtWidgets.QMessageBox.NoRole)
        msgbox.exec_()
        if msgbox.clickedButton() == botonno:
            return False
        else:
            return True
    

    【讨论】:

      【解决方案3】:

      使用functools 覆盖窗口小部件closeEvent 而不创建子类,然后使用event.accept()event.ignore() 接受或取消关闭事件:

      import functools
      import sys
      from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget, QLabel, QMessageBox
      
      
      def closeEvent(self, event):
          choice = QMessageBox.question(self, "Quit", "Do you want to quit chit chat?", QMessageBox.Yes | QMessageBox.No)
          if choice == QMessageBox.Yes:
              event.accept()
          else:
              event.ignore()
      
      
      app = QApplication(sys.argv)
      
      v_box = QVBoxLayout()
      
      window = QWidget()
      
      window.closeEvent = functools.partial(closeEvent, window)
      
      label = QLabel("Hello World")
      
      v_box.addWidget(label)
      
      window.setLayout(v_box)
      window.show()
      sys.exit(app.exec())
      

      【讨论】:

        猜你喜欢
        • 2014-11-11
        • 1970-01-01
        • 1970-01-01
        • 2013-08-27
        • 1970-01-01
        • 1970-01-01
        • 2015-11-20
        • 1970-01-01
        • 2021-05-30
        相关资源
        最近更新 更多