【问题标题】:Can't use python Tkinter无法使用 python Tkinter
【发布时间】:2021-07-22 21:48:33
【问题描述】:

我一直在尝试将 Tkinter 导入我的程序,但我不断收到错误消息:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/homebrew/Cellar/python@3.9/3.9.6/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'

当我尝试python3 -m tkinter:

File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 188, in _run_module_as_main
    mod_name, mod_spec, code = _get_module_details(mod_name, _Error)
  File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 147, in _get_module_details
    return _get_module_details(pkg_main_name, error)
  File "/opt/homebrew/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/runpy.py", line 111, in _get_module_details
    __import__(pkg_name)
  File "/opt/homebrew/Cellar/python@3.9/3.9.6/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'

我尝试使用 pip/python 安装 Tkinter,但它不起作用。以前它可以工作,但在运行之后

ln -s /opt/homebrew/bin/dot /usr/local/bin/dot

它停止工作了。

【问题讨论】:

  • 那台计算机上有图形用户界面吗?还有你是怎么安装python的?
  • tkinter 是 stript 语言 tcl 及其 GUI 模块 tk 的包装器,您可能必须使用 homebrew 安装它们 - 但通常应该安装所有需要的 `tkinter元素。

标签: python tkinter homebrew


【解决方案1】:

我阅读了“选择”,所以我假设您使用的是 Linux。您的问题的解决方案应该是:

sudo apt-get install python3-tk 

更新:只是现在,稍后,我注意到你使用import _tkinter。那不好。您应该使用import tkinter,因为使用 import _tkinter 您可以导入基于 C 的 cpython 库,并且您无权访问 Python 类

【讨论】:

    【解决方案2】:

    首先,你有 python 3 或更高版本吗? 你导入 tkinter 了吗?

    试试这个:py -3 -m pip install tkinter(适用于 Windows)

    【讨论】:

    • 安装只适用于windows,对不起
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-03
    • 2018-05-30
    • 2020-06-13
    • 2021-07-18
    • 2022-08-24
    • 2022-07-04
    • 1970-01-01
    相关资源
    最近更新 更多