将 python 脚本转换为 EXE 后,为什么 tkinter toplevel 的图标不显示

After converting a python script to EXE why tkinter toplevel's icon not showing

我正在尝试使用 tkiter 制作一个简单的 python GUI。最近我在使用 tkinter Toplevel 设置它的 window 图标时遇到了很大的麻烦。当我首先设置 Toplevel 的 window 图标时,它 运行 在 IDE 或 CMD 上很好,然后没有问题,当我将它转换为 EXE 时,没有显示图标除此之外没有显示其他小部件。起初尝试使用 cx_Freeze 并看到问题然后我尝试使用 pyinstaller 并且也有同样的问题。我也曾尝试通过互联网解决它,但一无所获。 代码在这里。

from tkinter import *
from tkinter import Toplevel
from PIL import Image

def show():
    window  = Toplevel(root, bg= '#4e6c9a')
    window.title('Ok')
    window.tk.call('wm', 'iconbitmap', window._w, 'Key.ico')
    bu = Button(window, text='me', command=window.destroy)
    bu.pack()

root = Tk()
bt = Button(text = 'click', command=show)
bt.pack()
root.mainloop()

它 运行 在 IDE 或 CMD 中很好。 正在使用 pyinstaller 将其转换为 EXE.... 在控制台中

pyinstaller -w -F --clean -n Removeer -i logo.ico main.py
78 INFO: PyInstaller: 4.1
78 INFO: Python: 3.8.7
78 INFO: Platform: Windows-7-6.1.7601-SP1
78 INFO: wrote F:\GUI\Removeer.spec
78 INFO: UPX is not available.
78 INFO: Removing temporary files and cleaning cache in C:\Users\IT Lab\AppData\Roaming\pyinstaller
93 INFO: Extending PYTHONPATH with paths
['F:\GUI', 'F:\GUI']
93 INFO: checking Analysis
93 INFO: Building Analysis because Analysis-00.toc is non existent
93 INFO: Initializing module dependency graph...
93 INFO: Caching module graph hooks...
109 INFO: Analyzing base_library.zip ...
2995 INFO: Processing pre-find module path hook distutils from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks\pre_find_module_path\hook-distutils.py'
.
2995 INFO: distutils: retargeting to non-venv dir 'C:\Python\lib'
5803 INFO: Caching module dependency graph...
5943 INFO: running Analysis Analysis-00.toc
5943 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by f:\GUI\venv\scripts\python.exe
5959 INFO: Analyzing F:\GUI\main.py
6099 INFO: Processing pre-safe import module hook urllib3.packages.six.moves from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks\pre_safe_import_modul
e\hook-urllib3.packages.six.moves.py'.
8112 INFO: Processing module hooks...
8112 INFO: Loading module hook 'hook-certifi.py' from 'f:\GUI\venv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\stdhooks'...
8127 INFO: Loading module hook 'hook-difflib.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8127 INFO: Excluding import of doctest from module difflib
8127 INFO: Loading module hook 'hook-distutils.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8127 INFO: Loading module hook 'hook-distutils.util.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8127 INFO: Excluding import of lib2to3.refactor from module distutils.util
8127 INFO: Loading module hook 'hook-encodings.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8205 INFO: Loading module hook 'hook-heapq.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8221 INFO: Excluding import of doctest from module heapq
8221 INFO: Loading module hook 'hook-lib2to3.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8361 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8361 INFO: Excluding import of test.support from module multiprocessing.util
8361 INFO: Excluding import of test from module multiprocessing.util
8361 INFO: Loading module hook 'hook-pickle.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8361 INFO: Excluding import of argparse from module pickle
8361 INFO: Loading module hook 'hook-PIL.Image.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8720 INFO: Loading module hook 'hook-PIL.ImageFilter.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8720 INFO: Excluding import of numpy from module PIL.ImageFilter
8720 INFO: Loading module hook 'hook-PIL.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8720 INFO: Import to be excluded not found: 'FixTk'
8720 INFO: Excluding import of tkinter from module PIL.ImageTk
8720 INFO: Excluding import of PyQt5 from module PIL.ImageQt
8736 INFO: Loading module hook 'hook-PIL.SpiderImagePlugin.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8736 INFO: Import to be excluded not found: 'FixTk'
8736 INFO: Loading module hook 'hook-sysconfig.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8736 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8736 INFO: Loading module hook 'hook-xml.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8782 INFO: Loading module hook 'hook-_tkinter.py' from 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks'...
8907 INFO: checking Tree
8907 INFO: Building Tree because Tree-00.toc is non existent
8907 INFO: Building Tree Tree-00.toc
8970 INFO: checking Tree
8970 INFO: Building Tree because Tree-01.toc is non existent
8970 INFO: Building Tree Tree-01.toc
9032 INFO: checking Tree
9032 INFO: Building Tree because Tree-02.toc is non existent
9032 INFO: Building Tree Tree-02.toc
9063 INFO: Looking for ctypes DLLs
9110 INFO: Analyzing run-time hooks ...
9110 INFO: Including run-time hook 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth_multiprocessing.py'
9110 INFO: Including run-time hook 'f:\GUI\venv\lib\site-packages\PyInstaller\hooks\rthooks\pyi_rth__tkinter.py'
9110 INFO: Including run-time hook 'f:\GUI\venv\lib\site-packages\_pyinstaller_hooks_contrib\hooks\rthooks\pyi_rth_certifi.py'
9126 INFO: Looking for dynamic libraries
9796 INFO: Looking for eggs
9796 INFO: Using Python library C:\Python\python38.dll
9796 INFO: Found binding redirects:
[]
9812 INFO: Warnings written to F:\GUI\build\Removeer\warn-Removeer.txt
9890 INFO: Graph cross-reference written to F:\GUI\build\Removeer\xref-Removeer.html
9921 INFO: checking PYZ
9921 INFO: Building PYZ because PYZ-00.toc is non existent
9921 INFO: Building PYZ (ZlibArchive) F:\GUI\build\Removeer\PYZ-00.pyz
10951 INFO: Building PYZ (ZlibArchive) F:\GUI\build\Removeer\PYZ-00.pyz completed successfully.
10966 INFO: checking PKG
10966 INFO: Building PKG because PKG-00.toc is non existent
10966 INFO: Building PKG (CArchive) PKG-00.pkg
14492 INFO: Building PKG (CArchive) PKG-00.pkg completed successfully.
14523 INFO: Bootloader f:\GUI\venv\lib\site-packages\PyInstaller\bootloader\Windows-32bit\runw.exe
14523 INFO: checking EXE
14523 INFO: Building EXE because EXE-00.toc is non existent
14523 INFO: Building EXE from EXE-00.toc
14523 INFO: Copying icons from ['logo.ico']
14523 INFO: Writing RT_GROUP_ICON 0 resource with 20 bytes
14523 INFO: Writing RT_ICON 1 resource with 194968 bytes
14523 INFO: Updating manifest in F:\GUI\build\Removeer\runw.exe.9trd9vqt
14523 INFO: Updating resource type 24 name 1 language 0
14523 INFO: Appending archive to EXE F:\GUI\dist\Removeer.exe
14539 INFO: Building EXE from EXE-00.toc completed successfully.

完成后,应用程序将启动,然后当 Toplevel 启动时,您可以打开一个空白 window 并带有标题,但没有图标或小部件。我知道这是为了设置图标,因为当我在代码中删除图标并重新处理转换 EXE 方法时,顶层工作正常。请帮忙给我一个好的解决方案!

对不起,我语法不好

使用 cx_freeze 并将文件添加到 setup.py 文件 代码

import sys
from cx_Freeze import setup, Executable

# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {

"packages": [
'tkinter',
],
    'include_files': [r'icon'] # Here you must add the icon file
}

# GUI applications require a different base on Windows (the default is for
# a console application).
base = None
if sys.platform == "win32":
    base = "Win32GUI"

setup(  name = "Gui",
        version = "1.0",
        description = "A simple Description",
        options = {"build_exe": build_exe_options},
        executables = [Executable("script.py", base=base, icon=r'icon\icon.ico')])