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
angel725
V2EX  ›  Python

pyinstaller 打包 exe. 运行时反复打开自己

  •  
  •   angel725 · 2018-10-03 12:40:37 +08:00 · 2706 次点击
    这是一个创建于 2004 天前的主题,其中的信息可能已经有所发展或是发生改变。
    import welc
    
    class MyWindow(QMainWindow, Ui_MainWindow):
        def __init__(self, parent=None):
            super(MyWindow, self).__init__(parent)
            self.setupUi(self)
            with open('qss.qss', 'r') as q:
                self.setStyleSheet(q.read())
            self.open.clicked.connect(lambda:self.aa(1))
    
        def aa(self, ss):
            p = Process(target=welc.mo)
            p.start()
    
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        m = MyWindow()
        m.show()
        sys.exit(app.exec_())
    

    这是 pyqt5 做的 gui。 使用 pyinstaller 打包 exe。

    在开发环境中运行是正常的。

    但是打包成 exe 后,在点 open 按钮,它只是反复打开自己(就是 m.show()这的主窗口。)

    而不是打开进程中的方法。

    这是什么原因?

    3 条回复    2020-03-01 15:11:44 +08:00
    leoleoasd
        1
    leoleoasd  
       2018-10-03 13:24:39 +08:00   ❤️ 2
    我 打 开 我 自 己
    justou
        2
    justou  
       2018-10-03 14:21:09 +08:00 via Android   ❤️ 1
    pyinstaller+process 在 Windows 下需要特殊处理。好了,搜索关键字已知了
    zhaolei891220
        3
    zhaolei891220  
       2020-03-01 15:11:44 +08:00
    @justou 特意登录账号前来道谢,这个问题没有关键词很难搜索,一直不知道到底是什么问题,你这个关键字很好用,非常感谢
    附答案:
    新版 python3
    修改方式比较简单,在 if __name__=='__main__:'下添加一句 multiprocessing.freeze_support() 即可。
    如下:

    if __name__=='__main__':
    # 在此处添加
    multiprocessing.freeze_support()
    # 这里是你的代码
    # ......
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2844 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 71ms · UTC 13:43 · PVG 21:43 · LAX 06:43 · JFK 09:43
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.