【问题标题】:python code doesn't work correctly after compiling with pyinstaller使用 pyinstaller 编译后 python 代码无法正常工作
【发布时间】:2021-05-23 22:54:12
【问题描述】:

我正在尝试将我的tcountdown.py 编译成带有pyinstaller.exe 文件。我已经阅读了一些手册并观看了有关它的 YouTube 教程,并且都说相同(我做了):[cmd: c:/**/*>pyinstaller --onefile -w -F tcountdown.py]。 cmd 与 .py 的路径相同,它编译一个 .exe 文件。但是当我运行tcountdown.exe 时,它不会启动 tpopup。当我使用 cmd 运行 tcountdown.py 时,一切正常。我还只将 tpopup 编译为 .exe 以检查是否有问题,但这也可以正常工作。

tcountdown.py:

import os
import time

time.sleep(10)
os.system('tpopup.py')

主线:tcountdown.py

辅助脚本:tpopup.py

Python 版本是 3.9.1

【问题讨论】:

  • 请将您的标题更改为更具描述性的内容(并且正确...)。 Python 总是完全做它应该做的事情。问题可能在于你告诉它做什么...
  • 请注意,在程序中“运行”另一个 Python 文件的正确方法是导入它。使用os.system 意味着您有两个单独的 程序,它们恰好都是用Python 编写的。由于 pyinstaller 打包了 a Python 程序,它可能不会同时打包这两个程序。
  • @Tomerikoo:对不起,我不擅长创作标题
  • @MisterMiyagi:但是当我导入 tpopup.py 时,它会立即打开,这是不应该的。我希望 tpopup 在执行 tcountdown 后启动

标签: python python-3.x pyinstaller


【解决方案1】:

我认为您需要编辑 tpopup.py 以便它具有您需要为设置执行的任何设置:

# your setup code goes where it was here

然后,将其余代码放入一个函数中,例如:

def open_popup(maybe_with_arguments):
    # rest of the code here

然后在tcountdown中,可以使用

import tpopup # with import, you don't need to compile tpopup - you just need to make sure it's in the same directory as tcountdown.py and pyinstaller should notice it and pack it into the .exe
import time
time.sleep(10) # waiting
tpopup.open_popup(possible_arguments) # calls the code that actually calls the popup, opening the popup AFTER the wait has been completed.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-05
    • 2021-09-26
    • 1970-01-01
    • 2014-08-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多