久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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

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

        <tfoot id='B8KmE'></tfoot>
        <i id='B8KmE'><tr id='B8KmE'><dt id='B8KmE'><q id='B8KmE'><span id='B8KmE'><b id='B8KmE'><form id='B8KmE'><ins id='B8KmE'></ins><ul id='B8KmE'></ul><sub id='B8KmE'></sub></form><legend id='B8KmE'></legend><bdo id='B8KmE'><pre id='B8KmE'><center id='B8KmE'></center></pre></bdo></b><th id='B8KmE'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='B8KmE'><tfoot id='B8KmE'></tfoot><dl id='B8KmE'><fieldset id='B8KmE'></fieldset></dl></div>

      1. <legend id='B8KmE'><style id='B8KmE'><dir id='B8KmE'><q id='B8KmE'></q></dir></style></legend>

        捕獲 QApplication 中引發的異常

        Catching exceptions raised in QApplication(捕獲 QApplication 中引發的異常)
            <tbody id='cuBRa'></tbody>
          <tfoot id='cuBRa'></tfoot>
            <bdo id='cuBRa'></bdo><ul id='cuBRa'></ul>
            • <i id='cuBRa'><tr id='cuBRa'><dt id='cuBRa'><q id='cuBRa'><span id='cuBRa'><b id='cuBRa'><form id='cuBRa'><ins id='cuBRa'></ins><ul id='cuBRa'></ul><sub id='cuBRa'></sub></form><legend id='cuBRa'></legend><bdo id='cuBRa'><pre id='cuBRa'><center id='cuBRa'></center></pre></bdo></b><th id='cuBRa'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='cuBRa'><tfoot id='cuBRa'></tfoot><dl id='cuBRa'><fieldset id='cuBRa'></fieldset></dl></div>

                  <legend id='cuBRa'><style id='cuBRa'><dir id='cuBRa'><q id='cuBRa'></q></dir></style></legend>

                1. <small id='cuBRa'></small><noframes id='cuBRa'>

                  本文介紹了捕獲 QApplication 中引發的異常的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我正在嘗試使用 PyQt5 編寫一個在系統托盤中運行的應用程序.代碼有時會引發異常,我需要能夠捕獲它們.

                  I'm trying to write an app that works in system tray using PyQt5. The code is sometimes raising exceptions, and I need to be able to catch them.

                  我希望當應用程序中發生異常時,主事件循環會退出,所以像這樣捕獲它應該可以工作:

                  I would expect that when an exception occurs in an app, the main event loop is exited, so catching it like that should work:

                  try:
                      application.exec()
                  except:
                      do_stuff()
                  

                  在下面的例子中,當我按下Raise"按鈕時,我只看到回溯,但我從來沒有看到 error catched! 打印出來.

                  In the following example, when I press the "Raise" button, I only see the traceback, but I never see the error catched! printed.

                  from PyQt5 import QtWidgets, QtGui, QtCore
                  
                  class ErrorApp():
                      def __init__(self):
                          # Init QApplication, QWidet and QMenu
                          self.app = QtWidgets.QApplication([])
                          self.widget = QtWidgets.QWidget()
                          self.menu = QtWidgets.QMenu("menu", self.widget)
                  
                          # Add items to menu
                          self.menu_action_raise = self.menu.addAction("Raise")
                          self.menu_action_raise.triggered.connect(self.raise_error)
                  
                          self.menu_action_exit = self.menu.addAction("Exit")
                          self.menu_action_exit.triggered.connect(self.app.exit)
                  
                          # Create the tray app
                          self.tray = QtWidgets.QSystemTrayIcon(QtGui.QIcon("logo.png"), self.widget)
                          self.tray.setContextMenu(self.menu)
                  
                          # Show app
                          self.tray.show()
                  
                      def raise_error(self):
                          assert False
                  
                  e = ErrorApp()
                  
                  try:
                      e.app.exec()
                  
                  except:
                      print("error catched!")
                  

                  有 2 個類似的問題,但那里的答案沒有做我需要做的事情:

                  There are 2 similar questions, but the answers there don't do what I need to do:

                  抓取 PyQt 中的任何異常:OP 想要監控 異常,偶數循環不退出防止 PyQt 靜默插槽中發生的異常:裝飾器回答根本行不通;將 sys.exit(1) 添加到 sys.excepthook 只會關閉整個程序,而不會打印 error catched!

                  Grab any exception in PyQt: the OP wants to monitor the exceptions, the even loop isn't exited Preventing PyQt to silence exceptions occurring in slots: the decorator answer simply doesn't work; adding sys.exit(1) to sys.excepthook just closes the whole program, without printing error catched!

                  推薦答案

                  你必須使用異常,如果你想要事件循環結束,那么你必須調用 quit()(或 exit()) 方法.

                  You must use the exception and, if you want the event loop to end then you must call the quit()(or exit()) method.

                  import sys
                  import traceback
                  from PyQt5 import QtWidgets, QtGui, QtCore
                  
                  
                  class ErrorApp:
                      # ...
                  
                      def raise_error(self):
                          assert False
                  
                  
                  def excepthook(exc_type, exc_value, exc_tb):
                      tb = "".join(traceback.format_exception(exc_type, exc_value, exc_tb))
                      print("error catched!:")
                      print("error message:
                  ", tb)
                      QtWidgets.QApplication.quit()
                      # or QtWidgets.QApplication.exit(0)
                  
                  
                  sys.excepthook = excepthook
                  e = ErrorApp()
                  ret = e.app.exec_()
                  print("event loop exited")
                  sys.exit(ret)
                  

                  輸出:

                  error catched!:
                  error message:
                   Traceback (most recent call last):
                    File "main.py", line 28, in raise_error
                      assert False
                  AssertionError
                  
                  event loop exited
                  

                  這篇關于捕獲 QApplication 中引發的異常的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                  PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                  How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                  `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                  <legend id='ArsSn'><style id='ArsSn'><dir id='ArsSn'><q id='ArsSn'></q></dir></style></legend>
                2. <small id='ArsSn'></small><noframes id='ArsSn'>

                  <i id='ArsSn'><tr id='ArsSn'><dt id='ArsSn'><q id='ArsSn'><span id='ArsSn'><b id='ArsSn'><form id='ArsSn'><ins id='ArsSn'></ins><ul id='ArsSn'></ul><sub id='ArsSn'></sub></form><legend id='ArsSn'></legend><bdo id='ArsSn'><pre id='ArsSn'><center id='ArsSn'></center></pre></bdo></b><th id='ArsSn'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ArsSn'><tfoot id='ArsSn'></tfoot><dl id='ArsSn'><fieldset id='ArsSn'></fieldset></dl></div>

                      • <bdo id='ArsSn'></bdo><ul id='ArsSn'></ul>
                          <tbody id='ArsSn'></tbody>
                          • <tfoot id='ArsSn'></tfoot>
                          • 主站蜘蛛池模板: 欧美日韩精品综合 | 亚洲一区二区免费视频 | 国产成人精品一区二区三区在线观看 | 国产露脸国语对白在线 | 日韩久久综合 | 日韩中文字幕一区二区 | 浮生影院免费观看中文版 | 午夜看片| 作爱视频免费看 | 欧美亚洲国产日韩 | 日韩爱爱网站 | 五月综合激情在线 | 欧美一区二区三区四区视频 | 久久99精品国产 | 日日想夜夜操 | 精品一区二区三区在线观看 | 国产精品极品美女在线观看免费 | 91免费在线播放 | 一区二区成人在线 | 韩日一区二区三区 | 国产免费一区二区 | 欧美一区二区三区四区视频 | 色接久久 | 国产成人精品免费 | 成人精品在线观看 | 国产精华一区 | 日韩午夜电影在线观看 | 亚洲一区在线日韩在线深爱 | 男女啪啪高潮无遮挡免费动态 | 精精国产xxxx视频在线播放 | 91视视频在线观看入口直接观看 | 国产在线视频一区 | 日本电影一区二区 | 成人在线免费网站 | 人人鲁人人莫人人爱精品 | 国产视频在线一区二区 | 天天曰夜夜 | 日本不卡免费新一二三区 | 一区二区三区四区在线 | 天天干天天插天天 | 精品久久免费 |