1 回答

TA貢獻(xiàn)1773條經(jīng)驗 獲得超3個贊
是的,使用 Tkinter 和 SQLite 制作可執(zhí)行文件是完全可能的。
Pyinstaller
在虛擬環(huán)境中安裝后,您應(yīng)該創(chuàng)建一個main.spec
具有以下格式的文件,以便您有足夠的自由來完全自定義您的 exe 文件。根據(jù)您的代碼,您可能需要更少或更多的功能:
# -*- mode: python -*-
import os
block_cipher = None
current_dir = os.path.dirname(os.path.curdir)
a = Analysis(['main.py'],
? ? ? ? ? ? ?# pathex=['output_path'],
? ? ? ? ? ? ?pathex=[current_dir],
? ? ? ? ? ? ?binaries=[],
? ? ? ? ? ? ?datas=[
? ? ? ? ? ? ? ? ?('img/*.png', 'img'),
? ? ? ? ? ? ? ? ?('inputs/*.csv', 'inputs'),
? ? ? ? ? ? ? ? ?('databases/*.db', 'databases'),
? ? ? ? ? ? ? ? ?('settings.ini', '.'),
? ? ? ? ? ? ?],
? ? ? ? ? ? ?hiddenimports=[],
? ? ? ? ? ? ?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,
? ? ? ? ? a.binaries,
? ? ? ? ? a.zipfiles,
? ? ? ? ? a.datas,
? ? ? ? ? Tree('documentation/_build/html', prefix='documentation/_build/html/'),
? ? ? ? ? name='Project Name',
? ? ? ? ? debug=False,
? ? ? ? ? strip=False,
? ? ? ? ? upx=True,
? ? ? ? ? console=False)
您應(yīng)該正確設(shè)置output_path和Project Name。此外,此腳本假定您的主文件名為main.py,但您也可以更改它。如您所見,在這種情況下,我捆綁了所有圖像、csv 文件、數(shù)據(jù)庫 (SQLite) 甚至從 Sphinx 創(chuàng)建的文檔。
然后,您必須調(diào)用以下命令:
path_to_pyinstaller/pyinstaller --onefile main.spec --key your_key
path_to_pyinstaller你的virtualenv下安裝Pyinstaller的路徑在哪里。您還必須設(shè)置your_key.
還有其他庫也可以使用,例如cx_Freeze,但同樣,我通常使用Pyinstaller.
重要的是要記住,在捆綁時,可能會出現(xiàn)一些與相對路徑相關(guān)的錯誤。我的解決方案是定義一個resource_path這樣的函數(shù):
import sys
# Get the absolute path
def resource_path(relative_path):
? ? """ Get absolute path to resource, works for dev and for PyInstaller """
? ? try:
? ? ? ? # PyInstaller creates a temp folder and stores path in _MEIPASS
? ? ? ? base_path = sys._MEIPASS
? ? except Exception as e:
? ? ? ? # print(e)
? ? ? ? base_path = os.path.abspath(".")
? ? return os.path.join(base_path, relative_path)
然后,在調(diào)用任何圖像、文件或數(shù)據(jù)庫時都應(yīng)該使用此函數(shù)。例如,當(dāng)連接到 SQLite 時,您應(yīng)該執(zhí)行以下操作:
import sqlite3
self.conn = sqlite3.connect(resource_path(database_path))
database_path數(shù)據(jù)庫的相對路徑在哪里。
添加回答
舉報