我一直在Stack Overflow和其他网站上搜索如何将数据文件添加到我的Python应用程序中:
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打包成单个文件,请帮忙。谢谢。
我使用的是安装有Python 2.7和PyInstaller 3.2的Windows 10计算机。
pyinstaller
使用临时文件夹来提取文件。你需要在代码中为冻结的应用程序指定,就像这里所做的那样 here。 - Repiklisapp.iconbitmap(resource_path('/M.ico'))
? - Michael Zhengresources
中,并在代码底部添加答案中的两行代码(在设置图标之前)。如果你仍然有问题,请告诉我。 - Repiklisresources=['M.ico'])
,并在我的实际Python文件中添加了if hasattr(sys, '_MEIPASS'): os.chdir(sys._MEIPASS)
,但我仍然收到错误消息:_tkinter.TclError: bitmap "C:\Users\micha\AppData\Local\Temp\_MEI69~1/M.ico" not defined
。 - Michael Zhengos.sep
来获取系统路径分隔符。 - Repiklis