【问题标题】:Getting tkinter to work with python 3.x on macos with asdf让 tkinter 在带有 asdf 的 macos 上使用 python 3.x
【发布时间】:2020-03-19 01:24:40
【问题描述】:

所以我很难过。如何让 python 3.7.x 与 tkinter 和 asdf 一起工作?

我做了以下事情:

1) asdf local python 3.7.4

2)brew install tcl-tk

3)brew link tcl-tk --force

4) python -m venv --system-site-packages nltk

我有一些类似的代码:

import nltk
from nltk.corpus import wordnet as wn
from tkinter import *

# Let's get the first sense of vehicle
vehicle = wn.synsets('vehicle')[0]
# Let's build a concept tree
t = nltk.Tree(vehicle.name(), children=[
    nltk.Tree(vehicle.hyponyms()[3].name(), children=[]),
    nltk.Tree(vehicle.hyponyms()[4].name(), children=[]),
    nltk.Tree(vehicle.hyponyms()[5].name(), children=[]),
    nltk.Tree(vehicle.hyponyms()[7].name(), children=[
        nltk.Tree(vehicle.hyponyms()[7].hyponyms()[1].name(), children=[]),
        nltk.Tree(vehicle.hyponyms()[7].hyponyms()[3].name(), children=[]),
        nltk.Tree(vehicle.hyponyms()[7].hyponyms()[4].name(), children=[]),
        nltk.Tree(vehicle.hyponyms()[7].hyponyms()[5].name(), children=[]), nltk.Tree(vehicle.hyponyms()[7].hyponyms()[6].name(), children=[]),
        ]),
    ])
t.draw()

然后我使用 nltk 库运行包含上述代码的 python 脚本来绘制概念树。我得到以下输出:

Traceback (most recent call last):
  File "concept_tree.py", line 3, in <module>
    from tkinter import *
  File "/Users/alexander/.asdf/installs/python/3.7.4/lib/python3.7/tkinter/__init__.py", line 36, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ModuleNotFoundError: No module named '_tkinter'

【问题讨论】:

    标签: python python-3.x tkinter


    【解决方案1】:

    asdf python 在后台使用 pyenv,因此您可以使用所有相同的构建选项。

    在 pyenv 中,您需要在安装命令前添加以下内容才能使用 tkinter 进行安装:

    PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'"
    

    这也可以添加到asdf install python 3.7.4

    PYTHON_CONFIGURE_OPTS="--with-tcltk-includes='-I/usr/local/opt/tcl-tk/include' --with-tcltk-libs='-L/usr/local/opt/tcl-tk/lib -ltcl8.6 -ltk8.6'" asdf install python 3.7.4
    

    请注意,这需要在全新安装的 python 上完成。如果您已经安装了 python,则需要先卸载并重新启动,然后再使用 configure opts 运行命令。

    【讨论】:

      猜你喜欢
      • 2021-07-18
      • 2020-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-02-17
      • 2022-01-04
      • 1970-01-01
      • 2021-07-30
      相关资源
      最近更新 更多