简体   繁体   English

在使用py2app构建PySide应用程序包时,QApplication没有在主线程中运行

[英]QApplication is not running in main thread when building PySide app bundle with py2app

I am having problems building my PySide Python app using py2app (for OS X). 我在使用py2app构建我的PySide Python应用程序时遇到了问题(对于OS X)。 It appears that something funny happens with threads on the app bundle. 似乎应用程序包上的线程发生了一些有趣的事情。

Here is the minimal example 这是最小的例子

from PySide.QtCore import *
from PySide.QtGui import *
import sys

class App(QApplication):
    def __init__(self):
        QApplication.__init__(self, sys.argv, True)

        self.timer = QTimer(self)


if __name__=='__main__':
    app = App()
    app.exec_()

When run from the command line: python test.py , this works fine without error. 从命令行运行时: python test.py ,这样可以正常运行而不会出错。 However when I then compile it with the following setup.py: 然而,当我使用以下setup.py编译它时:

from setuptools import setup
import py2app
import PySide

APP = ['test.py']
DATA_FILES = []
OPTIONS = {'argv_emulation': False,
            'includes' : 'PySide',
            'resources' : "qt_menu.nib"
            }

setup(
    app=APP,
    data_files=DATA_FILES,
    options={'py2app': OPTIONS},
    setup_requires=['py2app'],
)

these errors appear in the Console: 这些错误出现在控制台中:

11/05/2013 13:54:20.958 [0x0-0xb37b37].org.pythonmac.unspecified.test: QObject: Cannot create children for a parent that is in a different thread.
11/05/2013 13:54:20.958 [0x0-0xb37b37].org.pythonmac.unspecified.test: (Parent is App(0x105f41f10), parent's thread is QThread(0x100106cc0), current thread is QThread(0x10251ea80)

So it appears that App is not being constructed to live in the main thread any more. 所以似乎App不再被构建为继续存在于主线程中。 Any ideas how to fix this? 任何想法如何解决这一问题?

The problem seems to be the way PySide manage the QThreads. 问题似乎是PySide管理QThreads的方式。 Yo're creating a QTimer with a QApplication as parent. 你正在以QApplication为父母创建一个QTimer When using PyQt4 that's not as problem, but it might be on PySide. 当使用PyQt4 ,这不是问题,但它可能在PySide上。

A QTimer also spawns a QThread so, try run your code without creating the QTimer . QTimer也会生成QThread因此请尝试运行代码而不创建QTimer

Note: At the time you asked the question, this could be a bug. 注意:当您提出问题时,这可能是一个错误。 And might be fixed in the last versions of PySide. 并且可能会在PySide的最新版本中修复。 (I'm just speculating :D) (我只是猜测:D)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM