问题描述
限时送ChatGPT账号..我试图运行一个使用乌龟画树的代码.代码没有问题,因为我在windows 10上运行成功,但是在ubuntu上,遇到如下错误:
I tried to run a code which use turtle to draw a tree. There is no problem with code because I ran it on Windows 10 successfully, but on Ubuntu, the following error was encountered:
mosharraf@mmh:~$ cd Desktop
mosharraf@mmh:~/Desktop$ python3 Tree.py
Traceback (most recent call last):
File "Tree.py", line 6, in <module>
import turtle
File "/usr/local/lib/python3.6/turtle.py", line 107, in <module>
import tkinter as TK
File "/usr/local/lib/python3.6/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'
mosharraf@mmh:~/Desktop$
然后我尝试安装 tkinter 但我失败了.
Then I tried to install tkinter but I failed.
mosharraf@mmh:~$ sudo apt-get install python3-tk
[sudo] password for mosharraf:
Reading package lists... Done
Building dependency tree
Reading state information... Done
python3-tk is already the newest version (3.5.1-1).
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
mosharraf@mmh:~$
之后,我检查了目录中的 tkinter 并找到了它.
Afterwards, I checked for the tkinter in the directories and found it.
mosharraf@mmh:/usr/local/lib/python3.6$ cd tkinter
mosharraf@mmh:/usr/local/lib/python3.6/tkinter$ ls
colorchooser.py constants.py dnd.py font.py __main__.py __pycache__ simpledialog.py tix.py
commondialog.py dialog.py filedialog.py __init__.py messagebox.py scrolledtext.py test ttk.py
mosharraf@mmh:/usr/local/lib/python3.6/tkinter$ python3
Python 3.6.4 (default, Dec 22 2017, 18:44:45)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/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'
>>>
tkinter 位于您在上面看到的目录中,但我无法导入它.我尝试了几种方法,例如
tkinter is in the directories as you can see above, but I can't import it. I tried several ways like
import tkinter
import tkinter as tk
from tkinter import *
等
请注意,我在 python 2.7 中运行代码并成功运行.我还在 Windows 10、Python 3.6 上运行了代码.但在 Ubuntu 中,它导致了问题.
Note that I ran the code in python 2.7 and it ran successfully. I also ran the code on Windows 10, Python 3.6. But in Ubuntu, it's causing the problem.
推荐答案
在你的帖子底部你说:
请注意,我在 python 2.7 中运行代码并成功运行.
Note that I ran the code in python 2.7 and it ran successfully.
当您从 Python 2 导入 Tkinter 时,您必须说:
When you import Tkinter from Python 2 you must say:
导入 Tkinter
当您从 Python 3 导入 Tkinter 时,您必须说:
When you import Tkinter from Python 3 you must say:
导入 tkinter
当我知道我的脚本将从多个版本的 Python 运行时,我用于 Tkinter 的片段如下:
A snippet I use for Tkinter when I know that my script is going to be ran from multiple versions of Python is below:
import sys
py_version = int(sys.version[0])
# If the python version is lower than 3, use Python 2 import
if py_version < 3:
import Tkinter
# Otherwise use Python 3 import
else:
import tkinter
如果以上没有解决您的问题:
我在您的一条错误消息中看到它说
IF THE ABOVE DID NOT FIX YOUR PROBLEM:
I see that in one of your error messages it says
python3-tk 已经是最新版本(3.5.1-1).
但错误消息还说您的 Python 版本是 3.6
.
But the error message also says that your Python version is 3.6
.
尝试使用:sudo apt-get install python3.6-tk
这篇关于ubuntu 16.4 上的 tkinter 导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论