0

xml.etree.ElementTree.fromstring()で関数が呼び出されると、無限のブロックがありQThreadます。また、他の多くの呼び出しにより、QThreadがのようにブロックされmultiprocessing.Process()ます。それは純粋なブロックであり、例外や中断はないと言うことが重要です。

コードは次のとおりです(少し編集されていますが、ソースと同じ原則です)。

from PyQt4.QtGui import *
from Ui_mainwindow import Ui_MainWindow
import sys
import xml.etree

class Bruton(QThread):
    def __init__(self, mw):
        super(Bruton, self).__init__(mw) 
        self.mw = mw

    def run(self):
        print("This message I see.")
        tree = xml.etree.ElementTree.fromstring("<element>text</element>")
        print("But this one never.")

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)
        self.init_bruton()

    # When the form is shown...
    def showEvent(self, arg1):
        self.bruton.start()

    def init_bruton(self):
        self.bruton = Bruton(self)

app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())
4

2 に答える 2

2

投稿されたコードは実際には実行されませんが、いくつかの小さな変更を加えるだけで実行され、正常に動作します。変更を加えたコードは次のとおりです。

from PyQt4.QtGui import *
from PyQt4.QtCore import *
import sys
import xml.etree.ElementTree

class Bruton(QThread):
    def __init__(self, mw):
        super(Bruton, self).__init__(mw)
        self.mw = mw

    def run(self):
        print("This message I see.")
        tree = xml.etree.ElementTree.fromstring("<element>text</element>")
        print("But this one never.")

class MainWindow(QMainWindow):
    def __init__(self):
        QMainWindow.__init__(self)
        self.init_bruton()

    # When the form is shown...
    def showEvent(self, arg1):
        self.bruton.start()

    def init_bruton(self):
        self.bruton = Bruton(self)

app = QApplication(sys.argv)
mw = MainWindow()
mw.show()
sys.exit(app.exec_())

そしてここに出力があります:

$ python test.py 
This message I see.
But this one never.

これは、DebianUnstable上のPython2.6.6、PyQt44.8.3で使用されます。

ご使用の環境で試して、変更した例が機能するかどうかを確認できますか?もしそうなら、あなたはあなたの本当のコードのための解決策への道を進んでいます。=)

于 2011-04-15T15:32:51.090 に答える
0

ここに示したコードは短縮されています(ソースは2つのファイルに分割されています__ini__.py)。メインモジュールは、を開始するモジュールでなければならないことに気づきましたQApplication。そこで、プログラムのメインモジュールであるに追加app.exec_()しました。__init__.py

于 2011-04-15T16:13:20.887 に答える