在 Mac 上运行 Tkinter [英] Running Tkinter on Mac

查看:143
本文介绍了在 Mac 上运行 Tkinter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个绝对的新手.我正在尝试为我的学校项目制作 Python GUI,所以我决定使用 Tkinter.当我尝试导入 Tkinter 时,它会抛出此消息:

<预><代码>>>>导入 tkinter回溯(最近一次调用最后一次):文件<stdin>",第 1 行,在 <module> 中.文件/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py",第36行,在<module>import _tkinter # 如果失败,你的 Python 可能没有为 Tk 配置ModuleNotFoundError: 没有名为_tkinter"的模块

我试图在网上找到解决方案,但我无法弄清楚(主要是不明白).

我在 setup.py 中读到了一些目录问题,但我不明白如何修复它.我的 python3.7 文件夹中有 tkinter 文件夹.

我不太明白我发现的这些步骤:

<块引用>

如果它因No module named _tkinter"而失败,则需要修改您的 Python 配置以包含此模块(这是一个用 C 实现的扩展模块).不要不要编辑模块/设置(它已经过时).您可能必须安装 Tcl 和 Tk(使用 RPM 时,也安装 -devel RPM)和/或编辑 setup.py 脚本以指向安装 Tcl/Tk 的正确位置.如果您在默认位置安装 Tcl/Tk,只需重新运行make"即可.应该构建 _tkinter 扩展.

我使用的是 Mac OS 并使用 Visual Studio Code.

解决方案

要检查您的 Python 版本,请在终端上运行:

$ python --version

检查您的机器使用的是什么python.运行:

$ which python

如果它使用来自 Homebrew 的 python,它可能使用的是 Python 2.如果您手动安装了 python 3,只需手动安装 tKinter.

$ brew install python-tk

要手动运行 python 2,请在终端上运行:

$ python <文件名>

如果是python 3,运行:

$ python3 <文件名>

I am an absolute newbie. I'm trying to make Python GUI for my school project so I decided to use Tkinter. When I try to import Tkinter it throws this message:

>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.7/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'

I tried to find a solution online but I couldn't figure it out (mostly didn't understand it).

I read about some problem with directory in setup.py but I don't understand how to fix it. I have tkinter folder in my python3.7 folder.

I don't really understand these steps that I found:

If it fails with "No module named _tkinter", your Python configuration needs to be modified to include this module (which is an extension module implemented in C). Do not edit Modules/Setup (it is out of date). You may have to install Tcl and Tk (when using RPM, install the -devel RPMs as well) and/or edit the setup.py script to point to the right locations where Tcl/Tk is installed. If you install Tcl/Tk in the default locations, simply rerunning "make" should build the _tkinter extension.

I'm using Mac OS and use Visual Studio Code.

解决方案

To check your python version, run this on terminal:

$ python --version

Check what python your machine is using. Run:

$ which python

If it's using python from Homebrew, it's probably using Python 2. If you installed python 3 manually, just install tKinter manually.

$ brew install python-tk

To run python 2 manually, run on terminal:

$ python <FILENAME>

If python 3, run:

$ python3 <FILENAME>

这篇关于在 Mac 上运行 Tkinter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆