簡體   English   中英

tkinter window 啟動后自動關閉.exe-file

[英]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 筆記本中運行它時,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 出現錯誤消息(“無法執行腳本 test1_tkinter.exe”, - 上面提到的)並且 window 不會自動消失。

問候,so_question_asker

我相信使用窗口標志將解決問題:

pyinstaller file_name —onefile —windowed

很可能有一條錯誤消息,但它關閉得太快而無法閱讀。

為了能夠閱讀錯誤消息,請按照以下步驟操作:

  1. 使用 window 創建 exe 文件(如果還沒有的話)。
  2. 通過 Windows (CMD) 命令提示符運行 exe 文件,使用“cd folder_path”進入“test1_tkinter.exe”所在的文件夾。
  3. 輸入 test1_tkinter.exe 並輸入以運行應用程序。

錯誤消息應顯示在命令提示符 window 上,並且不會立即關閉,以便您進一步研究該問題。

編輯:

根據錯誤消息:pyinstaller 需要一個.py 文件才能創建一個functional.exe 文件,並且不支持從.ipynb 文件轉換。

最好從 jupyter notebook 下載文件(文件 > 下載為 > Python(.py)),然后將其轉換為 .exe 文件。

這個問題解決了

要運行它,您需要在 py 啟動器中正確運行。

但問題是 py 啟動器僅在需要獲取用戶輸入時運行(如 a=input()),否則它會嘗試運行代碼並關閉。

為避免這種情況,您需要設置一個空白的用戶輸入,例如某個變量 a=input("")。 如果您設置它,您的代碼將正確運行。

注意:只有在 cmd 中使用“pyinstaller 文件名”安裝它時,它才能更高效、更快速地運行

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM