tkinter window 在启动 .exe 文件后自动关闭

tkinter window closes automatically after starting .exe-file

现已解决

我写了一个脚本:

from tkinter import *

root= Tk()
root.title('Hello World')
text_label0 = Label(text="Hello World!")
text_label0.pack()


root.mainloop()

当我在 Jupyter notebook 中 运行 时,tkinter window 正确显示并且只有在您单击退出按钮时才会关闭。没有问题。

但后来我将脚本转换为 .exe 文件。它的名字是 test1_tkinter.exe。之前是 test1_tkinter.ipynb.

现在,当我双击 test1_tkinter.exe 时,windows 命令中心打开几毫秒后自动关闭。没有打开 tkinter window。

我把 test1_tkinter.ipynb 转换成 test1_tkinter.exe 的方法是,输入 windows 命令中心:

pyinstaller --onefile test1_tkinter.ipynb

它没有问题,因为那时创建了 test1_tkinter.exe。

所以我不知道问题的原因是什么。有人可以帮我吗?

编辑:Orangepeels,非常感谢您快速而简单的回答。当我按照你说的做的时候,出现了:

C:\Users\Lenovo>pyinstaller test1_tkinter.ipynb --onefile --windowed
107 INFO: PyInstaller: 4.1
107 INFO: Python: 3.9.1
107 INFO: Platform: Windows-10-10.0.18362-SP0
108 INFO: wrote C:\Users\Lenovo\test1_tkinter.spec
111 INFO: UPX is not available.
122 INFO: Extending PYTHONPATH with paths
['C:\Users\Lenovo', 'C:\Users\Lenovo']
132 INFO: checking Analysis
147 INFO: Building because C:\Users\Lenovo\test1_tkinter.ipynb changed
147 INFO: Initializing module dependency graph...
151 INFO: Caching module graph hooks...
171 INFO: Analyzing base_library.zip ...
3843 INFO: Processing pre-find module path hook distutils from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'.
3845 INFO: distutils: retargeting to non-venv dir 'c:\users\lenovo\appdata\local\programs\python\python39\lib'
6719 INFO: Caching module dependency graph...
6879 INFO: running Analysis Analysis-00.toc
6883 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by c:\users\lenovo\appdata\local\programs\python\python39\python.exe
6958 WARNING: lib not found: api-ms-win-core-path-l1-1-0.dll dependency of c:\users\lenovo\appdata\local\programs\python\python39\python39.dll
6976 INFO: Analyzing C:\Users\Lenovo\test1_tkinter.ipynb
6979 INFO: Processing module hooks...
6980 INFO: Loading module hook 'hook-difflib.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
6983 INFO: Excluding import of doctest from module difflib
6983 INFO: Loading module hook 'hook-distutils.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
6986 INFO: Loading module hook 'hook-distutils.util.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
6989 INFO: Excluding import of lib2to3.refactor from module distutils.util
6989 INFO: Loading module hook 'hook-encodings.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
7107 INFO: Loading module hook 'hook-heapq.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
7109 INFO: Excluding import of doctest from module heapq
7110 INFO: Loading module hook 'hook-lib2to3.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
7180 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
7182 INFO: Excluding import of test from module multiprocessing.util
7182 INFO: Excluding import of test.support from module multiprocessing.util
7183 INFO: Loading module hook 'hook-pickle.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
7185 INFO: Excluding import of argparse from module pickle
7186 INFO: Loading module hook 'hook-sysconfig.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
7188 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
7189 INFO: Loading module hook 'hook-xml.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
7270 INFO: Loading module hook 'hook-_tkinter.py' from 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks'...
7447 INFO: checking Tree
7519 INFO: checking Tree
7628 INFO: checking Tree
7662 INFO: Looking for ctypes DLLs
7705 INFO: Analyzing run-time hooks ...
7709 INFO: Including run-time hook 'c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_multiprocessing.py'
7720 INFO: Looking for dynamic libraries
8038 INFO: Looking for eggs
8039 INFO: Using Python library c:\users\lenovo\appdata\local\programs\python\python39\python39.dll
8040 INFO: Found binding redirects:
[]
8044 INFO: Warnings written to C:\Users\Lenovo\build\test1_tkinter\warn-test1_tkinter.txt
8112 INFO: Graph cross-reference written to C:\Users\Lenovo\build\test1_tkinter\xref-test1_tkinter.html
8129 INFO: checking PYZ
8144 INFO: Building because toc changed
8144 INFO: Building PYZ (ZlibArchive) C:\Users\Lenovo\build\test1_tkinter\PYZ-00.pyz
8880 INFO: Building PYZ (ZlibArchive) C:\Users\Lenovo\build\test1_tkinter\PYZ-00.pyz completed successfully.
8903 INFO: checking PKG
8906 INFO: Building because toc changed
8906 INFO: Building PKG (CArchive) PKG-00.pkg
10964 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
10967 INFO: Bootloader c:\users\lenovo\appdata\local\programs\python\python39\lib\site-packages\PyInstaller\bootloader\Windows-64bit\runw.exe
10967 INFO: checking EXE
10979 INFO: Building because console changed
10980 INFO: Building EXE from EXE-00.toc
10982 INFO: Appending archive to EXE C:\Users\Lenovo\dist\test1_tkinter.exe
11135 INFO: Building EXE from EXE-00.toc completed successfully.

但是当我打开.exe文件时,出现错误信息: “无法执行脚本 test1_tkinter.exe”

我现在可以做什么?

编辑 2: 当我通过命令提示符打开 test1_tkinter.exe 时,命令提示符显示如下:

C:\Users\Lenovo\dist>test1_tkinter.exe
Traceback (most recent call last):
  File "test1_tkinter.ipynb", line 22, in <module>
NameError: name 'null' is not defined
[7536] Failed to execute script test1_tkinter

供您参考:只有 9 行,脚本中没有第 22 行。

这仅在我使用“pyinstaller --onefile test1_tkinter”创建 .exe 文件时出现。当我使用“pyinstaller test1_tkinter --onefile --windowed”时,会通过一个额外的小 window 并且 window 不会自动消失。

您好, so_question_asker

我相信使用窗口标志会解决问题:

pyinstaller file_name —onefile —windowed

很可能有一条错误消息,但它关闭得太快以至于无法阅读。

为了能够阅读错误消息,请按照以下步骤操作:

  1. 用 window 创建 exe 文件(如果还没有)。
  2. 运行 通过 Windows (CMD) 命令提示符使用 'cd folder_path' 进入 'test1_tkinter.exe' 所在文件夹的 exe 文件。
  3. 输入 test1_tkinter.exe 并进入 运行 应用程序。

错误消息应显示在命令提示符 window 上并且不会立即关闭,以便您可以进一步研究该问题。

编辑:

根据错误消息:pyinstaller 需要 .py 文件才能创建功能性 .exe 文件,并且不支持从 .ipynb 文件转换。

最好是从 jupyter notebook 下载文件(文件 > 下载为 > Python(.py)),然后将其转换为 .exe 文件。