<i id='9oCIj'><tr id='9oCIj'><dt id='9oCIj'><q id='9oCIj'><span id='9oCIj'><b id='9oCIj'><form id='9oCIj'><ins id='9oCIj'></ins><ul id='9oCIj'></ul><sub id='9oCIj'></sub></form><legend id='9oCIj'></legend><bdo id='9oCIj'><pre id='9oCIj'><center id='9oCIj'></center></pre></bdo></b><th id='9oCIj'></th></span></q></dt></tr></i><div id='9oCIj'><tfoot id='9oCIj'></tfoot><dl id='9oCIj'><fieldset id='9oCIj'></fieldset></dl></div>
    <legend id='9oCIj'><style id='9oCIj'><dir id='9oCIj'><q id='9oCIj'></q></dir></style></legend>

          <bdo id='9oCIj'></bdo><ul id='9oCIj'></ul>

        <tfoot id='9oCIj'></tfoot>

        <small id='9oCIj'></small><noframes id='9oCIj'>

        PyQt5 closeEvent 方法

        时间:2023-08-05

        1. <i id='uaii5'><tr id='uaii5'><dt id='uaii5'><q id='uaii5'><span id='uaii5'><b id='uaii5'><form id='uaii5'><ins id='uaii5'></ins><ul id='uaii5'></ul><sub id='uaii5'></sub></form><legend id='uaii5'></legend><bdo id='uaii5'><pre id='uaii5'><center id='uaii5'></center></pre></bdo></b><th id='uaii5'></th></span></q></dt></tr></i><div id='uaii5'><tfoot id='uaii5'></tfoot><dl id='uaii5'><fieldset id='uaii5'></fieldset></dl></div>

              <bdo id='uaii5'></bdo><ul id='uaii5'></ul>

              <small id='uaii5'></small><noframes id='uaii5'>

              • <tfoot id='uaii5'></tfoot>
                  <tbody id='uaii5'></tbody>

                  <legend id='uaii5'><style id='uaii5'><dir id='uaii5'><q id='uaii5'></q></dir></style></legend>
                  本文介绍了PyQt5 closeEvent 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我目前正在学习如何使用 pyqt5 构建应用程序,但遇到了 closeEvent 方法的一些问题,已被覆盖,因此 QMessageBox 对象要求用户确认.它似乎与 X 按钮配合得很好——当操作被确认时,事件被接受",当点击取消按钮时,事件被取消".但是,当我从下拉文件菜单中使用退出按钮时,无论我单击哪个按钮,程序都会以退出代码 1 关闭.看起来很奇怪,因为我在两种情况下都使用相同的 closeEvent 方法.

                  I'm currently learning how to build an application with pyqt5 and encountered some problem with closeEvent method, overriden so user gets asked for confirmation by QMessageBox object. It seems working well with X button - event gets 'accepted' when action is confirmed and 'canceled' when cancel button is clicked. However, when I use my Quit button from dropdown File menu, no matter which button I click, program gets closed with exit code 1. Seems strange, because I use same closeEvent method in both cases.

                  import sys
                  
                  from PyQt5.QtWidgets import QApplication, QMessageBox, QMainWindow, QAction
                  
                  
                  class window(QMainWindow):
                      def __init__(self):
                  
                          super().__init__()
                  
                      def createUI(self):
                  
                  
                          self.setGeometry(500, 300, 700, 700)
                  
                          self.setWindowTitle("window")
                  
                  
                          quit = QAction("Quit", self)
                          quit.triggered.connect(self.closeEvent)
                  
                          menubar = self.menuBar()
                          fmenu = menubar.addMenu("File")
                          fmenu.addAction(quit)
                  
                      def closeEvent(self, event):
                          close = QMessageBox()
                          close.setText("You sure?")
                          close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
                          close = close.exec()
                  
                          if close == QMessageBox.Yes:
                              event.accept()
                          else:
                              event.ignore()
                  
                  main = QApplication(sys.argv)
                  window = window()
                  window.createUI()
                  window.show()
                  sys.exit(main.exec_())
                  

                  感谢您的建议!

                  推荐答案

                  当您单击按钮时,程序会调用您的函数,但使用不同的 event 对象,该对象没有 accept()ignore() 所以你会收到错误消息并且程序以退出代码 1 结束.

                  When you click button then program calls your function but with different event object which doesn't have accept() and ignore() so you get error message and program ends with exit code 1.

                  您可以分配 self.close 并且程序将使用正确的事件对象调用 closeEvent().

                  You can assign self.close and program will call closeEvent() with correct event object.

                  quit.triggered.connect(self.close)
                  

                  这篇关于PyQt5 closeEvent 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:如何在 python 和 qml 中自动插入/编辑 QAbstractList 下一篇:在 QGraphicsView 中播放图像序列(神秘的内存泄漏

                  相关文章

                  最新文章

                • <tfoot id='lp2GP'></tfoot><legend id='lp2GP'><style id='lp2GP'><dir id='lp2GP'><q id='lp2GP'></q></dir></style></legend>

                    <bdo id='lp2GP'></bdo><ul id='lp2GP'></ul>

                  <small id='lp2GP'></small><noframes id='lp2GP'>

                  1. <i id='lp2GP'><tr id='lp2GP'><dt id='lp2GP'><q id='lp2GP'><span id='lp2GP'><b id='lp2GP'><form id='lp2GP'><ins id='lp2GP'></ins><ul id='lp2GP'></ul><sub id='lp2GP'></sub></form><legend id='lp2GP'></legend><bdo id='lp2GP'><pre id='lp2GP'><center id='lp2GP'></center></pre></bdo></b><th id='lp2GP'></th></span></q></dt></tr></i><div id='lp2GP'><tfoot id='lp2GP'></tfoot><dl id='lp2GP'><fieldset id='lp2GP'></fieldset></dl></div>