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

如何解决将 python 脚本转换为 EXE 后,为什么 tkinter 顶层的图标不显示

我正在尝试使用 tkiter 制作一个简单的 Python GUI。最近我在使用 tkinter Toplevel 设置它的窗口图标时遇到了很大的麻烦。当我首先设置 Toplevel 的窗口图标时,它在 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.

编译后应用程序将启动,然后当顶层启动时,您可以打开一个带有标题的空白窗口,但没有图标或没有小部件。我知道这是用于设置图标,因为当我删除代码中的图标并重新处理转换 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')])

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-
参考1 参考2 解决方案 # 点击安装源 协议选择 http:// 路径填写 mirrors.aliyun.com/centos/8.3.2011/BaseOS/x86_64/os URL类型 软件库URL 其他路径 # 版本 7 mirrors.aliyun.com/centos/7/os/x86
报错1 [root@slave1 data_mocker]# kafka-console-consumer.sh --bootstrap-server slave1:9092 --topic topic_db [2023-12-19 18:31:12,770] WARN [Consumer clie
错误1 # 重写数据 hive (edu)> insert overwrite table dwd_trade_cart_add_inc > select data.id, > data.user_id, > data.course_id, > date_format(
错误1 hive (edu)> insert into huanhuan values(1,'haoge'); Query ID = root_20240110071417_fe1517ad-3607-41f4-bdcf-d00b98ac443e Total jobs = 1
报错1:执行到如下就不执行了,没有显示Successfully registered new MBean. [root@slave1 bin]# /usr/local/software/flume-1.9.0/bin/flume-ng agent -n a1 -c /usr/local/softwa
虚拟及没有启动任何服务器查看jps会显示jps,如果没有显示任何东西 [root@slave2 ~]# jps 9647 Jps 解决方案 # 进入/tmp查看 [root@slave1 dfs]# cd /tmp [root@slave1 tmp]# ll 总用量 48 drwxr-xr-x. 2
报错1 hive> show databases; OK Failed with exception java.io.IOException:java.lang.RuntimeException: Error in configuring object Time taken: 0.474 se
报错1 [root@localhost ~]# vim -bash: vim: 未找到命令 安装vim yum -y install vim* # 查看是否安装成功 [root@hadoop01 hadoop]# rpm -qa |grep vim vim-X11-7.4.629-8.el7_9.x
修改hadoop配置 vi /usr/local/software/hadoop-2.9.2/etc/hadoop/yarn-site.xml # 添加如下 <configuration> <property> <name>yarn.nodemanager.res