V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
4ever911
V2EX  ›  Python

app 对象创建后,是不是不建议在主窗口显示之前做太多太费时间的工作?

  •  
  •   4ever911 · 2016-10-08 00:45:23 +08:00 · 1612 次点击
    这是一个创建于 2779 天前的主题,其中的信息可能已经有所发展或是发生改变。
    app 对象创建后,是不是不建议在主窗口显示之前做太多太费时间的工作?

    比如,我的代码如下:

    def main()

    //1.
    app = QApplication(sys.argv)

    //2.
    w = MainWindow()
    w.show()

    sys.exit(app.exec_())


    这里如果 MainWindow 的 Init 做太多事情, 哪怕是 sleep(600) ,也就是 sleep 5 分钟,程序到 sys.exit 这里会报错。
    如果我把那部分读取数据的操作放到 app 创建之前,就没问题。

    也就是说 sleep(600) 放在 1 没问题,放在 2 就出错。

    是有什么讲究吗? 我记得以前用 MFC 的时候,也是不建议在创建主窗口的过程中做太多太费时的事情。
    4 条回复    2016-10-08 21:50:52 +08:00
    justou
        1
    justou  
       2016-10-08 08:57:56 +08:00
    做一些会卡界面的操作放到单独线程更好, 比如在界面显示的同时要加载大量初始化数据,把加载数据的工作放在单独线程, 不阻塞界面的显示, 但是最好给出一个模态窗口提示数据正在加载,让用户稍等;
    或者在主界面显示之前用闪屏提示数据正在加载,有 photoshop 的话打开看看它的界面显示过程
    wangxkww
        2
    wangxkww  
       2016-10-08 14:07:24 +08:00
    sys.exit 报什么错误呢?
    zhuangzhuang1988
        3
    zhuangzhuang1988  
       2016-10-08 16:04:24 +08:00
    不可能...
    4ever911
        4
    4ever911  
    OP
       2016-10-08 21:50:52 +08:00
    @zhuangzhuang1988

    Please see code below:


    ```python

    from PyQt4 import QtGui, QtCore
    import sys
    import time

    app = QtGui.QApplication(sys.argv)

    #the following line will crash the app
    time.sleep(300)

    w = QtGui.QDialog()
    w.show()

    sys.exit(app.exec_())

    ```python


    Error Message: ICE default IO error handler doing an exit(), pid = 31805, errno = 32


    In some certain occasion, when tasks that take so much time for running, such as loading a database...etc, the app crashes either. To make it simple here, I just added a short period of time sleep to the source code, which crashes the app.


    Sorry about that I cannot type Chinese on my Linux Server.
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   2552 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 03:18 · PVG 11:18 · LAX 20:18 · JFK 23:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.