当前位置: 首页 > 知识库问答 >
问题:

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

柴俊捷
2023-03-14

我正在尝试用tkiter制作一个简单的python GUI。最近,我有一个很大的麻烦tkinter Toplevel设置它的窗口图标。当我首先设置顶级窗口图标时,它在IDE或CMD上运行良好,然后就没有问题了;当我将其转换为EXE时,除了它之外,没有显示图标,也没有显示其他小部件。首先尝试cx_冻结,看到问题后,我尝试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.

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

对不起,我的语法不好

共有1个答案

金晗日
2023-03-14

使用cx\U冻结并在安装时添加文件。把代码归档

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')])
 类似资料:
  • 问题内容: 我试图将一个相当简单的Python程序转换为可执行文件,但是找不到我想要的东西,所以我有几个问题(我正在运行Python3.6): 到目前为止,我发现这样做的方法如下 下载旧版本的Python并使用 在3.6中设置虚拟环境,这将允许我执行1。 下载Python到C ++转换器并使用它。 这是我尝试过的/遇到的问题。 我在安装所需的下载之前安装了它(pypi-something),所以它

  • 我创建了一个python脚本,并想把它给我的朋友。所以我去youtube看了这个视频(在尝试了很多其他视频之后)。我运行它,得到一个薄层色谱库错误。没问题,我找到了解决这个问题的答案,就在这里的堆栈流中。我输入这段代码(将Python35更改为36,并确保我的薄层色谱位于它所说的相同位置)。我运行它,但得到一个不同的错误,在我的cmd提示符中说 我只是想把我的游戏发送给朋友<请帮忙。我正在使用Py

  • 有什么想法吗?

  • 问题内容: 我在文件夹和tmx文件中有3个python脚本和许多图像,我想将其作为单个.exe。我无法找到如何将多个python脚本和文件夹转换为单个exe。我只能为单个python脚本找到cxfreeze和类似的东西。请帮忙。 提前致谢。 问题答案: 在cxfreeze中创建一个setup.py文件。您必须在cxfreeze可执行文件中将多个pyhton文件作为列表传递。引用此线程- 适用于两个

  • 问题内容: 我有一个用于MySQL数据库的初始化脚本,但出于测试目的,我不想使用H2数据库。有人知道如何转换文件,或者至少有语法差异列表吗?谢谢。 问题答案: 有许多数据库工具可帮助将数据从一个数据库迁移到另一个数据库,例如: 飞路 SQuirreL DB复制插件

  • 路径没错啊,就是在static目录下的 图片啊