回答編集履歴

3

--add-data -> --add-binary に変更

2020/06/30 14:14

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ディレクトリ構成次第ですが、一例を挙げるとコマンドラインで指定する場合は
8
8
 
9
- --add-data"images/*.png;images" ← ; の左側がプロジェクト内で参照する画像のpath/ 右側が実行時展開されるフォルダ
9
+ --add-binary "images/*.png;images" ← ; の左側がプロジェクト内で参照する画像のpath/ 右側が実行時展開されるフォルダ
10
10
 
11
11
 
12
12
 

2

--onefile付きで実行時のファイルのPATHを解決する

2020/06/30 14:14

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -33,3 +33,69 @@
33
33
  デバッグ時は、大抵スクリプトファイルのある場所がカレントディレクトリですが、
34
34
 
35
35
  インストール後は、exeファイルの場所がカレントディレクトリとは限らない点にも注意。
36
+
37
+
38
+
39
+ 追記: コード側の対応方法 (resource_path 関数は[bundling-data-files-with-pyinstaller-onefile](https://stackoverflow.com/questions/7674790/bundling-data-files-with-pyinstaller-onefile) から借用
40
+
41
+
42
+
43
+ ```
44
+
45
+
46
+
47
+ import os
48
+
49
+ import sys
50
+
51
+ import tkinter as tk
52
+
53
+
54
+
55
+ def resource_path(relative_path):
56
+
57
+ try:
58
+
59
+ # PyInstaller creates a temp folder and stores path in _MEIPASS
60
+
61
+ base_path = sys._MEIPASS
62
+
63
+ except Exception:
64
+
65
+ base_path = os.path.abspath(".")
66
+
67
+
68
+
69
+ return os.path.join(base_path, relative_path)
70
+
71
+
72
+
73
+
74
+
75
+ IMG_PATH = resource_path("./images/sample.png")
76
+
77
+
78
+
79
+
80
+
81
+ def main():
82
+
83
+ root = tk.Tk()
84
+
85
+ photo = tk.PhotoImage(file=IMG_PATH)
86
+
87
+ button = tk.Button(root, image=photo)
88
+
89
+ button.pack()
90
+
91
+ root.mainloop()
92
+
93
+
94
+
95
+
96
+
97
+ if __name__ == '__main__':
98
+
99
+ main()
100
+
101
+ ```

1

余分なコードが残っていたので削除

2020/06/30 13:22

投稿

teamikl
teamikl

スコア8664

test CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
 
4
4
 
5
- 自動で認識されないので、これは追加で指定が必要ですね。--add-data, --add-binary
5
+ 自動で認識されないので、これは追加で指定が必要ですね。
6
6
 
7
7
  ディレクトリ構成次第ですが、一例を挙げるとコマンドラインで指定する場合は
8
8