获取 "ModuleNotFoundError" 尝试导入 Tkinter Canvas?

Getting "ModuleNotFoundError" trying to import Tkinter Canvas?

我正在尝试做 Marc Leese 在这里尝试做的同样的事情,但似乎没有得到明确的答案(至少 none 对我有用):trying to convert this file to python 3 and getting the error module not found

也就是说,在 Python 3+ 环境中使用找到的 solitaire.py 示例代码,例如这里:https://android.googlesource.com/toolchain/python/+/0d4194853e08d3244931523470331c00dfb94863/Python-2.7.3/Demo/tkinter/guido/solitaire.py

具体来说,我正在 运行宁 Python 3.7.9 到 Spyder 3.3.6。

当我尝试 运行 上面的代码时,我首先得到一个错误,说找不到 Tkinter。但是,当我将 Tkinter 更正为 tkinter 时,我仍然收到一条错误消息:

ModuleNotFoundError: No module named 'Canvas'

无论我使用 from tkinter import * 还是 from tkinter import Canvas 我都会收到同样的错误。

有问题的整个代码块是:

# Imports
import math
import random
from tkinter import *
from Canvas import Rectangle, CanvasText, Group, Window
# Fix a bug in Canvas.Group as distributed in Python 1.4.  The
# distributed bind() method is broken.  Rather than asking you to fix
# the source, we fix it here by deriving a subclass:
class Group(Group):
    def bind(self, sequence=None, command=None):
        return self.canvas.tag_bind(self.id, sequence, command)

我之前注意到使用 import * 的示例代码似乎不像示例中那样工作,但在此之前我一直设法找到解决方法。

如果有人可以解释 here/how 是怎么回事来修复它,我将非常感激!

您复制了一些非常古老的代码,这些代码显然依赖于名为“Canvas”的模块(例如,名为 Canvas.py 的文件)。这个模块不是 tkinter 的一部分。您需要找到原始 Canvas.py 文件