【问题标题】:tkinter window closes automatically after starting .exe-filetkinter 窗口在启动 .exe 文件后自动关闭
【发布时间】:2020-12-20 21:56:37
【问题描述】:

现已解决

我写了一个脚本:

from tkinter import *

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


root.mainloop()

当我在 Jupyter 笔记本中运行它时,tkinter 窗口会正确显示,并且只有在您单击退出按钮时才会关闭。没什么问题。

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

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

我将 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”时,会通过一个额外的小窗口出现错误消息(“无法执行脚本test1_tkinter.exe”, - 如上所述),并且该窗口不会自动消失。

您好, so_question_asker

【问题讨论】:

  • 在windows cmd中创建文件后,不要退出cmd,输入“test1_tkinter.exe”进行测试。这样您将看到导致程序崩溃的错误消息。
  • 感谢您的快速回答,小说。我试过了,你可以在“EDIT 2”之后的主帖中阅读结果。
  • 我根据(“EDIT 2”)更新了答案 [stackoverflow.com/a/65386056/11688889],请参见下文。祝你好运!
  • 非常感谢紫舍!现在它起作用了!谢谢你帮助我的毅力。感谢其他所有人,他们也试图解决我的问题!

标签: python tkinter window exe


【解决方案1】:

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

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

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

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

编辑:

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

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

【讨论】:

  • 非常感谢紫舍。我按照您的描述做了,并将结果发布到“EDIT 2”之后的主要评论中。请阅读上文。非常感谢。
  • 非常感谢紫舍!现在它起作用了!谢谢你帮助我的毅力。感谢其他所有人,他们也试图解决我的问题!
【解决方案2】:

我相信使用窗口化标志可以解决问题:

pyinstaller file_name —onefile —windowed

【讨论】:

  • 请在“编辑:”后的评论上方阅读我的原始帖子
  • 你是使用 conda 还是 pip 来安装 pyinstaller?这只是我的个人经验,但我在通过 conda 安装 pyinstaller 时遇到了问题。我能够通过使用带有 .py 文件而不是 .ipynb 文件的 pyinstaller 来运行您的代码。正如其他人所说,最好的办法是直接从打开的命令提示符运行 .exe,以便您可以阅读错误消息。
  • 谢谢你的回答,橘皮。我使用 pip 来安装 pyinstaller。我做了两次,因为我遇到了这个问题,但并没有好转。我将使用命令提示符尝试这个想法。谢谢。
  • 现在可以了。感谢所有参与帮助我的人!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-22
  • 1970-01-01
相关资源
最近更新 更多