这似乎是一个关于让 MacOS 指向正确版本的 tkinter 的长期问题。我有一个非常简单的 python 脚本,可以从 geeksforgeeks 绘制到画布。
from tkinter import *
root = Tk()
C = Canvas(root, bg="yellow",height=250, width=300)
line = C.create_line(108, 120,320, 40,fill="green")
arc = C.create_arc(180, 150, 80, 210, start=0,extent=220,fill="red")
oval = C.create_oval(80, 30, 140,150, fill="blue")
C.pack()
mainloop()
当我运行它时,
-> python tkTest2.py
它绘制了窗口,但它是空白的,并给出了可怕的终端错误:
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
单击窗口会显示:
2024-11-26 15:16:47.379 python[29220:77343080] +[IMKClient subclass]: chose IMKClient_Modern
2024-11-26 15:16:47.379 python[29220:77343080] +[IMKInputSession subclass]: chose IMKInputSession_Modern
我还没有找到可以解决我的问题的答案。我删除了 pyenv 和 tcl-tk brew 安装并按照 这些说明重新安装。
-> brew install tcl-tk
==> Caveats
==> tcl-tk
The sqlite3_analyzer binary is in the `sqlite-analyzer` formula.
-> brew install [email protected]
==> Caveats
==> [email protected]
[email protected] is keg-only, which means it was not symlinked into /opt/homebrew,
because this is an alternate version of another formula.
-> brew install pyenv
-> pyenv install 3.10.15 master
python-build: use openssl from homebrew
python-build: use readline from homebrew
Downloading Python-3.10.15.tar.xz...
-> https://www.python.org/ftp/python/3.10.15/Python-3.10.15.tar.xz
Installing Python-3.10.15...
python-build: use tcl-tk from homebrew
python-build: use readline from homebrew
python-build: use zlib from xcode sdk
Installed Python-3.10.15 to /Users/johnkomp/.pyenv/versions/3.10.15
-> pyenv versions master
system
* 3.10.15 (set by /Users/johnkomp/Programming/git/Stochastic-Games/RL-Apps/.python-version)
-> pyenv local 3.10.15 master
-> pyenv global 3.10.15
根据该反馈,我怀疑它使用自制程序中较新的 tcl/tk 正确构建。
我的路径是:
/opt/homebrew/Cellar/tcl-tk/9.0.0_1/bin:/usr/local/bin:/usr/local/bin:/Users/mike/.local/bin:/Users/mikesmith/.pyenv/shims:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Library/Apple/usr/bin:/Library/TeX/texbin
但是如果我运行这个简单的脚本:
import tkinter
root = tkinter.Tk()
print(root.tk.exprstring('$tcl_library'))
print(root.tk.exprstring('$tk_library'))
print(tkinter.TkVersion)
我得到:
-> python tkTest.py
DEPRECATION WARNING: The system version of Tk is deprecated and may be removed in a future release. Please don't rely on it. Set TK_SILENCE_DEPRECATION=1 to suppress this warning.
/System/Library/Frameworks/Tcl.framework/Versions/8.5/Resources/Scripts
/System/Library/Frameworks/Tk.framework/Versions/8.5/Resources/Scripts
8.5
/Users/mikesmith/.pyenv/versions/3.10.15/bin/python
-> which python
/Users/mikesmith/.pyenv/shims/python
所以它显然仍然指向旧版本。什么可能仍然指向那里?我必须重建框架吗?如何做到这一点?
我在 MacBook 上运行 Python 程序时遇到了与您类似的问题。此外,由于这些错误,我仅在完成以下操作后查看了在 Tkinter GUI 上创建的按钮。
错误: 2024-12-14 10:16:16.092 Python[89542:11677896] +[IMKClient 子类]: 选择 IMKClient_Modern 2024-12-14 10:16:16.092 Python[89542:11677896] +[IMKInputSession 子类]: 选择 IMKInputSession_Modern
执行的操作:
brew uninstall python
https://www.python.org/downloads/
运行完安装提示后,我可以成功查看计算器项目的按钮。让我知道这是否有帮助;谢谢!