简体   繁体   English

不能在 macOS 上的 Python 3 中使用 tkinter

[英]Cannot use tkinter in Python 3 on macOS

Short version: Python 3 is unable to use tkinter on my iMac.短版: Python 3 无法在我的 iMac 上使用 tkinter。

Details and useful info: I'm on macOS Mojave, Version 10.14.3 (18D109).详细信息和有用信息:我使用的是 macOS Mojave,版本 10.14.3 (18D109)。 (I can't upgrade for a while yet because of software that won't run on the newer versions.) (由于软件无法在较新版本上运行,我暂时无法升级。)

In my scripts I'm using #!/usr/bin/env python3 and when I get to the line import tk as tkinter I get this error:在我的脚本中,我使用#!/usr/bin/env python3 ,当我到达import tk as tkinter我收到此错误:

Traceback (most recent call last):
  File "/Users/hal/Documents/Dev/HalPy/TestScript.py", line 5, in <module>
    import tkinter
  File "/Users/hal/.pyenv/versions/3.7.3/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'

When I tested it, from the command line, I typed python and got the Python shell and the version info ( Python 3.7.3 (default, Apr 11 2020, 02:53:05) ). When I tested it, from the command line, I typed python and got the Python shell and the version info ( Python 3.7.3 (default, Apr 11 2020, 02:53:05) ). Then I tried import tkinter as tk and I get this:然后我尝试import tkinter as tk ,我得到了这个:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/hal/.pyenv/versions/3.7.3/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 see the module when I drop into the Help mode and type modules and tkinter shows up in the list of modules.当我进入帮助模式并键入modules时,我看到了该模块,tkinter 出现在模块列表中。 Just in case, I tried installing it with pip, but it wouldn't install (which is what I expected).以防万一,我尝试使用 pip 安装它,但它不会安装(这是我所期望的)。

So what do I need to do to get Python 3 to recognize tkinter?那么我需要做什么才能让 Python 3 识别 tkinter? (Just in case it matters, Python 2 with Tkinter does work.) (以防万一,Python 2 和 Tkinter 确实有效。)

Also, I have looked at this question and this one and this is NOT a duplicate.另外,我看过这个问题这个问题,这不是重复的。 (First note that neither has an accepted answer.) The first one involves reinstalling, and the focus seems to be on Windows, and this is on macOS, which includes it pre-installed. (首先请注意,两者都没有可接受的答案。)第一个涉及重新安装,重点似乎在 Windows 上,这是在 macOS 上,其中包括预安装。 I'm a bit concerned what reinstalling it would do.我有点担心重新安装它会做什么。 The 2nd question is having problems because it's being run within the XCode framework and one element of the issue is the wrong shebang and I'm already using the one the answer suggests.第二个问题有问题,因为它在 XCode 框架内运行,问题的一个要素是错误的 shebang,我已经在使用答案建议的那个。

you can find your answer here: https://blog.lanzani.nl/2020/install-tkinter-macos/你可以在这里找到你的答案: https://blog.lanzani.nl/2020/install-tkinter-macos/

If you are using a Python from any current python.org Python installer for macOS (3.9.0+, 3.8.0+, or 3.7.2+), no further action is needed to use IDLE or tkinter. If you are using a Python from any current python.org Python installer for macOS (3.9.0+, 3.8.0+, or 3.7.2+), no further action is needed to use IDLE or tkinter. A built-in version of Tcl/Tk 8.6 will be used.将使用 Tcl/Tk 8.6 的内置版本。

If you are using macOS 10.6 or later, the Apple-supplied Tcl/Tk 8.5 has serious bugs that can cause application crashes.如果您使用的是 macOS 10.6 或更高版本,Apple 提供的 Tcl/Tk 8.5 存在可能导致应用程序崩溃的严重错误。 If you wish to use IDLE or Tkinter, do not use the Apple-supplied Pythons.如果您希望使用 IDLE 或 Tkinter,请不要使用 Apple 提供的 Python。 Instead, install and use a newer version of Python from python.org or a third-party distributor that supplies or links with a newer version of Tcl/Tk.相反,安装和使用来自 python.org 的更新版本的 Python 或提供或链接到更新版本的 Tcl/Tk 的第三方分销商。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM