1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

PyInstaller+PyQtでタスクバーアイコンを設定する

Posted at

PyQtで作成したデスクトップアプリケーションに、タスクバーアイコンを設定する方法

この記事は9割がたこの記事を参考にしています

※ 元記事では.specファイルを変更しているが、その部分がそのままでは動作しなかったため、調べたところpyinstallerの引数で対応できたので備忘録として書いています

実装

1. アイコンファイルの準備

  • .ico形式
    • pngでも問題ないが、exeアイコンにもしたいので、サイトで変換しておく
  • main.pyと同じディレクトリに配置しておく
  • サイズ: 128x128とか

2. リソースパスの設定

PyInstallerでexe化した際にもアイコンファイルを正しく読み込めるよう、リソースパス取得用の関数つくる

def resource_path(relative):
    if hasattr(sys, '_MEIPASS'):
        # PyInstallerでexe化した場合のパス
        return os.path.join(sys._MEIPASS, relative)
    # 通常実行時のパス
    return os.path.join(os.path.abspath('.'), relative)

3. アプリケーションへのアイコン設定

app = QApplication(sys.argv)
app.setWindowIcon(QIcon(resource_path('image.ico')))

ちなみに実行時にステートによってアイコンを変更するみたいなことも可能

4. PyInstallerでのexe化

アイコンファイルを含めてexe化する際は以下のようにデータとして追加する必要がある

pyinstaller --add-data "image.ico;." --icon=image.ico -F -w main.py

オプションの説明:

  • --add-data: リソースファイルの追加
  • --icon: exe自体のアイコン設定
  • -F: 単一ファイル化
  • -w: コンソールウィンドウを非表示

終わり

タスクバーアイコンが無印だと何動かしてるのか分からないので、できると便利

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?