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.

这适用于 Debian Unstable 上的 Python 2.6.6、PyQt4 4.8.3。

您可以在您的环境中尝试一下,看看我修改后的示例是否适合您?如果是这样,您正在为您的真实代码找到解决方案。=)

于 2011-04-15T15:32:51.090 回答
0

我在这里显示的代码被缩短了(源代码分为两个文件和__ini__.py)。我注意到主模块必须是启动QApplication. 所以我添加app.exec_()__init__.pywhich 是我程序的主要模块。

于 2011-04-15T16:13:20.887 回答