ModuleNotFoundError: 在 macOS 上找不到名为 '_tkinter' 的模块

13

Tkinter不起作用,它抛出一个错误。

安装:

% pip3 install tk

我的代码:

#!/usr/bin/env python3

import tkinter as tk

错误:

Traceback (most recent call last):
  File "/Users/arghadip/Library/Application Support/CodeRunner/Unsaved/Untitled.py", line 4, in <module>
    import tkinter as tk
  File "/usr/local/Cellar/python@3.9/3.9.5/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 37, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

_tkinter, so your python might not be configured for tkinter - user15801675
我该如何配置它? - user15242190
2
brew install python-tk 尝试。 - user15801675
可以,它起作用了,但是你能告诉我pip3 install tk是什么吗? - user15242190
2
@user15242190,tk是PyPI上的TensorKit,不是Tkinter。https://pypi.org/project/tk/ - Brandon
3个回答

40

对于Python3,tkinter可以通过以下方式简单安装:

brew install python-tk

有时候在我的Mac上使用pip不能成功,特别是在High Sierra操作系统版本上。可以使用Brew在mac上安装各种软件包。


3
它对我起作用了。需要注意的一点是:我正在使用Python 3.10,因此在安装Tk时必须指定版本,如brew install python-tk@3.10,否则它将无法工作。 - Richard

0

我在Python2到Python3模块名称更改的问题上遇到了相同的问题。如前面的帖子所述,"brew install python-tk" 对我有效。我正在运行以下内容:

  • Python 3.9.13 64位
  • vsCode 1.68.0
  • macOS Monterey

0

如果您使用的是 macOS 系统,可以运行以下命令来安装 python-tk。我已在最新的 macOS Ventura 13.1 上进行了测试,运行良好。

sudo chown -R $(whoami) /usr/local/share /usr/local/share/man /usr/local/share/man/man8 chmod u+w /usr/local/share /usr/local/share/man /usr/local/share/man/man8

brew install python-tk


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接