有人用Pyinstaller成功地将数据文件绑定到一个文件中?

2024-04-26 13:11:01 发布

您现在位置:Python中文网/ 问答频道 /正文

关于如何将数据文件添加到python应用程序中,我一直在结合Stack Overflow和web上的其他部分:

import Tkinter

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
        self.grid()

--- Everything Fine Here ---

        self.B = Tkinter.Button(self, text = 'Create Document', command = self.OnButtonClick)
        self.B.grid(column = 0, row = 6)


    def OnButtonClick(self):
        createDoc()

if __name__ == "__main__":
    app = simpleapp_tk(None)
    app.title('Receipt Form')
    app.iconbitmap(os.getcwd() + '/M.ico')
    app.mainloop()

我尝试过使用.spec文件,但没有成功

Onedir工作正常,但是当我试图编译成一个可执行文件时,它会给出一个错误,即文件'M.ico'没有被定义。在

如果有人能够将数据文件与pyinstaller绑定到一个文件中。请帮忙。谢谢。在

我在一台运行python2.7和pyinstaller3.2的windows10计算机上


Tags: 文件selfappinittkinter数据文件deftk
1条回答
网友
1楼 · 发布于 2024-04-26 13:11:01

您必须指定要添加到pyinstaller.spec文件中的每个数据文件,或者通过命令行选项(.spec更容易。)下面是我的.spec文件,其中有一个“datas”部分:

# -*- mode: python -*-

block_cipher = None


a = Analysis(['pubdata.py'],
             pathex=['.', '/interface','/recommender'],
             binaries=None,
             datas=[('WordNet/*.txt','WordNet'),
             ('WordNet/*.json','WordNet'),
             ('WordNet/pdf_parsing/*.json','pdf_parsing'),
             ('WordNet/pdf_parsing/*.xml','pdf_parsing'),
             ('images/*.png','images'),
             ('database/all_meta/Flybase/*.txt','all_meta'),
             ('database/all_meta/Uniprot/*.txt','all_meta'),
             ('database/json_files/*.json','json_files'),
             ('Data.db','.')],

             hiddenimports=['interface','recommender'],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='GUI',
          debug=False,
          strip=False,
          upx=True,
          console=True )
coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=True,
               name='GUI')
app = BUNDLE(coll,
             name='App.app',
             icon=None)

在此之后,如果您试图访问在.spec文件中指定的任何数据文件,则必须使用Pyinstaller的\u MEIPASS文件夹来引用文件。下面是我如何使用名为数据数据库公司名称:

^{pr2}$

上述方法取代了这一单独的陈述:

conn = lite.connect("Data.db")

当我经历同样的事情时,这个链接对我帮助很大: https://irwinkwan.com/2013/04/29/python-executables-pyinstaller-and-a-48-hour-game-design-compo/

希望这有帮助!在

相关问题 更多 >