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

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

    <tfoot id='QQVGP'></tfoot>

    1. <legend id='QQVGP'><style id='QQVGP'><dir id='QQVGP'><q id='QQVGP'></q></dir></style></legend>
      1. <small id='QQVGP'></small><noframes id='QQVGP'>

        <i id='QQVGP'><tr id='QQVGP'><dt id='QQVGP'><q id='QQVGP'><span id='QQVGP'><b id='QQVGP'><form id='QQVGP'><ins id='QQVGP'></ins><ul id='QQVGP'></ul><sub id='QQVGP'></sub></form><legend id='QQVGP'></legend><bdo id='QQVGP'><pre id='QQVGP'><center id='QQVGP'></center></pre></bdo></b><th id='QQVGP'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='QQVGP'><tfoot id='QQVGP'></tfoot><dl id='QQVGP'><fieldset id='QQVGP'></fieldset></dl></div>
      2. 線程時出現 msgbox 錯誤,GUI 塊

        msgbox error while threading , GUI blocks(線程時出現 msgbox 錯誤,GUI 塊)
            <i id='CEBey'><tr id='CEBey'><dt id='CEBey'><q id='CEBey'><span id='CEBey'><b id='CEBey'><form id='CEBey'><ins id='CEBey'></ins><ul id='CEBey'></ul><sub id='CEBey'></sub></form><legend id='CEBey'></legend><bdo id='CEBey'><pre id='CEBey'><center id='CEBey'></center></pre></bdo></b><th id='CEBey'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='CEBey'><tfoot id='CEBey'></tfoot><dl id='CEBey'><fieldset id='CEBey'></fieldset></dl></div>
            <tfoot id='CEBey'></tfoot>

            <legend id='CEBey'><style id='CEBey'><dir id='CEBey'><q id='CEBey'></q></dir></style></legend>
              <tbody id='CEBey'></tbody>
          • <small id='CEBey'></small><noframes id='CEBey'>

                  <bdo id='CEBey'></bdo><ul id='CEBey'></ul>
                  本文介紹了線程時出現 msgbox 錯誤,GUI 塊的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我在執(zhí)行以下 gui 時遇到問題.如果沒有消息框,它可以正常工作,但是當有消息框時,它會阻塞.知道為什么有消息時gui會阻塞.謝謝

                  I have a problem while executing the following gui. it works normally if there's no msgbox, but when there is a mesbox it blocks. any idea why the gui blocks when there is message. thank you

                  from PyQt5 import QtCore, QtGui, QtWidgets
                  import threading
                  import time
                  class Ui_MainWindow(object):
                      def setupUi(self, MainWindow):
                          MainWindow.setObjectName("MainWindow")
                          MainWindow.resize(800, 600)
                          self.centralwidget = QtWidgets.QWidget(MainWindow)
                          self.centralwidget.setObjectName("centralwidget")
                          self.verticalLayout = QtWidgets.QVBoxLayout(self.centralwidget)
                          self.verticalLayout.setObjectName("verticalLayout")
                          self.pushButton = QtWidgets.QPushButton(self.centralwidget)
                          self.pushButton.setObjectName("pushButton")
                          self.verticalLayout.addWidget(self.pushButton)
                          MainWindow.setCentralWidget(self.centralwidget)
                          self.menubar = QtWidgets.QMenuBar(MainWindow)
                          self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
                          self.menubar.setObjectName("menubar")
                          MainWindow.setMenuBar(self.menubar)
                          self.statusbar = QtWidgets.QStatusBar(MainWindow)
                          self.statusbar.setObjectName("statusbar")
                          MainWindow.setStatusBar(self.statusbar)
                          self.retranslateUi(MainWindow)
                          QtCore.QMetaObject.connectSlotsByName(MainWindow)
                          self.pushButton.pressed.connect(self.threadingc)      
                      def calculation(self):
                          for i in range(10):
                              time.sleep(1)
                              print(i)
                          msg = QtWidgets.QMessageBox()
                          msg.setInformativeText('Finish')
                          msg.exec_()   
                      def threadingc(self):
                          x=threading.Thread(target=self.calculation)
                          x.start()
                      def retranslateUi(self, MainWindow):
                          _translate = QtCore.QCoreApplication.translate
                          MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
                          self.pushButton.setText(_translate("MainWindow", "PushButton"))
                  import sys
                  if __name__ == "__main__":
                      app = QtWidgets.QApplication(sys.argv)
                      MainWindow = QtWidgets.QMainWindow()
                      ui = Ui_MainWindow()
                      ui.setupUi(MainWindow)
                      MainWindow.show()
                      sys.exit(app.exec_())
                  

                  推薦答案

                  任何訪問 UI 元素只允許在主 Qt 線程內.請注意,access 不僅意味著讀取/寫入小部件屬性,還意味著創(chuàng)建;任何從其他線程執(zhí)行此操作的嘗試在最好的情況下都會導致圖形問題或不一致的行為,而在最壞(和更常見)的情況下會導致崩潰.

                  Any access to UI elements is only allowed from within the main Qt thread. Note that access means not only reading/writing widget properties, but also creation; any attempt to do so from other threads results in graphical issues or incosistent behavior in the best case, and a crash in the worst (and more common) case.

                  這樣做的唯一正確方法是使用帶有(可能)自定義信號的 QThread:這允許 Qt 正確地對信號進行排隊,并在它可以實際處理它們時對它們做出反應.

                  The only correct way to do so is to use a QThread with (possibly) custom signals: this allows Qt to correctly queue signals and react to them when it can actually process them.

                  以下是一個非常簡單的情況,不需要創(chuàng)建 QThread 子類,但請考慮這只是為了教育目的.

                  The following is a very simple situation that doesn't require creating a QThread subclass, but consider that this is just for educational purposes.

                  class Ui_MainWindow(object):
                      # ...
                  
                      def calculation(self):
                          for i in range(10):
                              time.sleep(1)
                              print(i)
                  
                      def showMessage(self):
                          msg = QtWidgets.QMessageBox()
                          msg.setInformativeText('Finish')
                          msg.exec_()   
                          self.pushButton.setEnabled(True)
                  
                      def threadingc(self):
                          self.pushButton.setEnabled(False)
                          self.thread = QtCore.QThread()
                          # override the `run` function with ours; this ensures that the function
                          # will be executed in the new thread
                          self.thread.run = self.calculation
                          self.thread.finished.connect(self.showMessage)
                          self.thread.start()
                  

                  請考慮以下重要方面:

                  • 我必須禁用按鈕,否則可以在前一個線程仍在執(zhí)行時創(chuàng)建一個新線程;這會產生問題,因為覆蓋 self.thread 會導致 python 在運行時嘗試垃圾收集(刪除)前一個線程,這是一個非常糟糕的 東西;
                  • 一個可能的解決方案是使用父線程創(chuàng)建線程,這通常使用簡單的 QThread(self) 來完成,但在您的情況下這是不可能的,因為 Qt 對象只能接受其他Qt 對象作為它們的父對象,而在您的情況下 self 將是一個 Ui_MainWindow 實例(這是一個基本的 python 對象);
                  • 以上幾點是一個重要問題,因為您嘗試從 pyuic 生成的文件開始執(zhí)行程序,而這永遠不會這樣做:這些文件是打算保持原樣而無需任何手動修改,并且僅用作導入的模塊;閱讀有關 使用 Designer 的官方指南中有關此主題的更多信息;另請注意,試圖模仿這些文件的行為是沒有用的,因為這通常會導致對象結構非常混亂;
                  • 理論上,您可以添加對 qt 對象的引用(例如,通過在 setupUi() 函數中添加 self.mainWindow = MainWindow)并使用該引用 (thread = QThread(self.mainWindow)),或將線程添加到持久列表 (self.threads = [],再次在 setupUi()),但由于上述原因,我強烈建議您不要這樣做;
                  • I had to disable the pushbutton, otherwise it would be possible to create a new thread while the previous one still executing; this will create a problem, since overwriting self.thread will cause python to try to garbage collect (delete) the previous thread while running, which is a very bad thing;
                  • a possible solution to this is to create the thread with a parent, which is usually done with a simple QThread(self), but that's not possible in your case because Qt objects can accept only other Qt objects as their parent, while in your case self would be a Ui_MainWindow instance (which is a basic python object);
                  • the above point is an important issue, because you're trying to implement your program starting from a pyuic generated file, which should never be done: those files are intended to be left as they are without any manual modification, and used only as imported modules; read more about this topic on the official guidelines about using Designer; also note that trying to mimic the behavior of those files is useless, as normally leads to great confusion about object structure;
                  • you could theoretically add a reference to a qt object (for example, by adding self.mainWindow = MainWindow in the setupUi() function) and create the thread with that reference (thread = QThread(self.mainWindow)), or add the thread to a persistent list (self.threads = [], again in the setupUi()), but due to the above point I strongly discourage you to do so;

                  最后,更正確代碼實現將要求您再次生成 ui 文件,保持原樣并執(zhí)行類似以下示例的操作;請注意,我添加了一個非常基本的異常實現,它還展示了如何正確地與自定義信號交互.

                  Finally, a more correct implementation of your code would require you to generate again the ui file, leave it as it is and do something like the following example; note that I added a very basic exception implementation that also shows how to correctly interact with custom signals.

                  from PyQt5 import QtCore, QtGui, QtWidgets
                  from mainwindow import Ui_MainWindow
                  import time
                  
                  class Calculation(QtCore.QThread):
                      error = QtCore.pyqtSignal(object)
                      def run(self):
                          for i in range(10):
                              time.sleep(1)
                              print(i)
                              try:
                                  10 / 0
                              except Exception as e:
                                  self.error.emit(e)
                                  break
                  
                  
                  class MainWindow(QtWidgets.QMainWindow, Ui_MainWindow):
                      def __init__(self):
                          super().__init__()
                          self.setupUi(self)
                          self.pushButton.pressed.connect(self.threadingc)
                  
                      def showMessage(self):
                          msg = QtWidgets.QMessageBox()
                          msg.setInformativeText('Finish')
                          msg.exec_()   
                  
                      def threadingc(self):
                          # create the thread with the main window as a parent, this is possible 
                          # since QMainWindow also inherits from QObject, and this also ensures
                          # that python will not delete it if you want to start another thread
                          thread = Calculation(self)
                          thread.finished.connect(self.showMessage)
                          thread.error.connect(self.showError)
                          thread.start()
                  
                  
                  if __name__ == "__main__":
                      import sys
                      app = QtWidgets.QApplication(sys.argv)
                      mainWindow = MainWindow()
                      mainWindow.show()
                      sys.exit(app.exec_())
                  

                  在上述情況下,使用以下命令處理 ui 文件(顯然,假設 ui 名為mainwindow.ui"):

                  In the above case, the ui file was processed using the following command (assuming that the ui is named "mainwindow.ui", obviously):

                  pyuic mainwindow.ui -o mainwindow.py
                  

                  這篇關于線程時出現 msgbox 錯誤,GUI 塊的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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時顯示進度條?)
                      <tbody id='AVEKN'></tbody>

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

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

                      • <legend id='AVEKN'><style id='AVEKN'><dir id='AVEKN'><q id='AVEKN'></q></dir></style></legend>
                          <bdo id='AVEKN'></bdo><ul id='AVEKN'></ul>

                            主站蜘蛛池模板: 久久免费精品视频 | 日韩在线观看一区 | 久久美女网 | 日韩视频在线一区 | 99精品免费视频 | 成人av电影在线 | 亚洲精品在线国产 | 羞羞网站在线观看 | 国产一区二区三区视频免费观看 | 欧美精品在线播放 | heyzo在线| 草草草影院 | 精品亚洲一区二区 | 欧美一级在线 | 看片地址 | 三级av在线 | 久久中文字幕一区 | 超碰在线免费av | 草草草网站 | 神马久久香蕉 | 国产精品久久欧美久久一区 | 91精品久久久久久久久久入口 | a国产一区二区免费入口 | 精产嫩模国品一二三区 | 国产精品久久久久久亚洲调教 | 淫片专区 | 国产精品九九九 | 久久国产视频网 | 在线观看h视频 | 美女久久视频 | 日本成人中文字幕在线观看 | 在线一区二区三区 | 天天玩天天干天天操 | 亚洲精品www | 男女网站在线观看 | 日韩中文字幕久久 | 精品久久久久久久人人人人传媒 | 日韩av黄色| 欧美成人免费 | av成人在线观看 | 91就要激情 |