環境
- MacOS 12.1
- Python 3.10.0
TkinterでGUI作るぞ!!!
エラーは出ないのにGUIの画面真っ黒やん。
ボタンも文字も何も出てこない。。。
あかん。 何やっても解決しないぞ、これ
と思って、別のライブラリを使って動かしたところ
You are running a VERY old version of tkinter 8.5.9.
You cannot use PNG formatted images for example. Please upgrade to 8.6.x
え? バージョン古いの??
実は気づくタイミングがありました。
直そうとしていた時に
Pythonの対話モードでバージョン確認したんですよ。
>>> tkinter.Tcl().eval('info patchlevel')
'8.5.9'
完全に見落としました。 Tcl/tkが入ってて満足してました。
MacOSでTkinter8.5系は表示バグの致命的欠陥があります。
よし!! アップデートするか!!
ターミナルにて brew info tcl-tk
と打つと
tcl-tk: stable 8.6.12 (bottled) [keg-only]
Not installed
入ってません。
なので brew install tcl-tk
で 最新版をインストール
Pythonはpyenv管理していたので
pyenv uninstall 3.10.0
pyenv install 3.10.0
GUI表示されたー!!!!
まとめ
Mac環境でTkinterのGUIが表示されないときはTcl-tkのバージョン確認しましょう。
pyenvで最新版のPythonをインストールしても
-> https://www.python.org/ftp/python/3.10.0/Python-3.10.0.tar.xz
Installing Python-3.10.0...
<中略>
python-build: use tcl-tk from homebrew
Tcl-tkは、Homebrewを使うので、brew
コマンドで確認しましょう
参考文献
Macのtkinter8.5でpngをまともに表示できない→PyenvのPython再インストールでtcl-tkをバージョンアップした