Skip to content

[已解決][Python] AttributeError: module ‘win32ctypes.pywin32.win32api’ has no attribute ‘error’

今天我在使用 PyInstaller 將 Python 程式轉成 Windows 上可以跑的 exe 執行檔時,發生了這個錯誤:

AttributeError: module 'win32ctypes.pywin32.win32api' has no attribute 'error'

解決方法

到網路上査了查,發現這個是很常見的錯誤:用來設定程式 Icon 的圖片必須為 .ico 檔,不能是 png 或其他格式的圖片。

要注意的是,這裡的 Icon 指的是應用程式在桌面顯示的那個 Icon ,不是打開後列在窗口上的 Icon。

那麼,最簡單的辦法,就是將打開瀏覽器,找個線上圖片格式轉換的網站,上傳、轉換、下載即可。

但這裡可以推薦一下 Python 當中的 Pillow —— 這個模組也可以用來將 png 圖片轉成 ico 檔。

# -*- coding: utf-8 -*-
from PIL import Image


image = Image.open('pic/NightyTimer_Icon.png')
print(image.size)




Output:

(32, 32)

這裡我的 png 檔案為 32 x 32 的解析度,我們直接儲存成 ico 檔即可。

image.save('test.ico', sizes=[(32, 32)])



要記得加上後面的 sizes 參數,不然格式可能會跑掉。成功轉換後,我們使用 PyInstaller 打包成執行檔就沒有問題了!


References


Read More

Tags:

Leave a Reply