【问题标题】:best strategy to improve the compatibility between Pyinstaller and Tkinter icon提高 Pyinstaller 和 Tkinter 图标兼容性的最佳策略
【发布时间】:2014-06-26 13:48:52
【问题描述】:

我对 py2exe 也有同样的问题。

我用 Tkinter 写了一个脚本。 GUI 的图标位图是一个 ico 图像“foologo.ico”,位于我计算机的特定目录 (C:\Users\fooimage) 中。当我使用 py2exe 创建可执行文件并在另一台 PC 中使用此可执行文件时,该软件无法运行,因为找不到图像

我正在寻找解决此问题的最佳策略。

from Tkinter import *

class MainWindow(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("foo")
        self.master.minsize(350, 150)
        self.master.wm_iconbitmap(default='C:\\Users\\fooimage\\foologo.ico')

if __name__=="__main__":
   d = MainWindow()
   d.mainloop()

我使用 Pyinstaller-2.01 将此脚本转换为可执行文件。

python pyinstaller.py --noconsole foo.py

一旦我有了可执行文件,如果我将 foologo.ico 文件移动到其他目录(或删除文件),则可执行文件将不起作用。当我发送我的可执行文件时也会发生问题

【问题讨论】:

标签: python user-interface directory runtime-error pyinstaller


【解决方案1】:

这是完整的规范文件:

# -*- mode: python -*-
a = Analysis(....)
pyz = PYZ(a.pure)
exe = EXE(pyz,
          a.scripts,
          a.binaries + [('your.ico', 'path_to_your.ico', 'DATA')], 
          a.zipfiles,
          a.datas, 
          name=....
       )

并在您的应用程序中使用它,如下所示:

datafile = "your.ico" 
if not hasattr(sys, "frozen"):   # not packed 
    datafile = os.path.join(os.path.dirname(__file__), datafile) 
else:  
    datafile = os.path.join(sys.prefix, datafile)

root = tk.Tk()
root.iconbitmap(default=datafile)       

【讨论】:

    猜你喜欢
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    相关资源
    最近更新 更多