【问题标题】:ImportError : No module named graphicsImportError:没有名为图形的模块
【发布时间】:2017-03-05 03:28:40
【问题描述】:

我尝试了一个需要图形包的图形算法,所以我导入了图形代码在这里

from graphics import *
import time

def BresenhamLine(x1,y1,x2,y2):
    """ Bresenham Line Drawing Algorithm For All Kind Of Slopes Of Line """

    dx = abs(x2 - x1)
    dy = abs(y2 - y1)
    slope = dy/float(dx)

    x, y = x1, y1   

    # creating the window
    win = GraphWin('Brasenham Line', 600, 480)

    # checking the slope if slope > 1 
    # then interchange the role of x and y
    if slope > 1:
        dx, dy = dy, dx
        x, y = y, x
        x1, y1 = y1, x1
        x2, y2 = y2, x2

    # initialization of the inital disision parameter
    p = 2 * dy - dx

    PutPixle(win, x, y)

    for k in range(2, dx):
        if p > 0:
            y = y + 1 if y < y2 else y - 1
            p = p + 2*(dy - dx)
        else:
            p = p + 2*dy

        x = x + 1 if x < x2 else x - 1

        # delay for 0.01 secs
        time.sleep(0.01)
        PutPixle(win, x, y)

def PutPixle(win, x, y):
    """ Plot A Pixle In The Windows At Point (x, y) """
    pt = Point(x,y)
    pt.draw(win)

def main():
    x1 = int(input("Enter Start X: "))
    y1 = int(input("Enter Start Y: "))
    x2 = int(input("Enter End X: "))
    y2 = int(input("Enter End Y: "))

    BresenhamLine(x1, y1, x2, y2)

if __name__ == "__main__":
    main()

但是当我执行这段代码时,它给了我这个错误

python app.py 
Traceback (most recent call last):
  File "app.py", line 1, in <module>
    from graphics import *
ImportError: No module named graphics

所以我尝试从 pip 安装图形包,但我也失败了。

sudo pip install graphics
Downloading/unpacking graphics
  Could not find any downloads that satisfy the requirement graphics
Cleaning up...
No distributions at all found for graphics
Storing debug log for failure in /home/ubuntu/.pip/pip.log

如何纠正这个错误?

【问题讨论】:

  • 询问谁编写了您正在从哪里工作的文档。

标签: python python-2.7 python-3.x graphics pip


【解决方案1】:

单击此处显示的链接。 https://mcsp.wartburg.edu/zelle/python/graphics.py 现在按照这个:选择所有文本数据>复制>保存(在工作文件夹中)>graphics.py>输入。创建一个类似 的新文件,编写代码并测试它。

【讨论】:

  • 这与the accepted answer 中的解决方案相同。 在回答已有答案的旧问题时,请确保提供新颖的解决方案或比现有答案更好的解释。
  • 这是一个清晰而恰当的问题答案。
【解决方案2】:

您必须下载this file 并将其与您的脚本放在同一个文件夹中。

来自文档字符串:

安装:将此文件放在 Python 可以看到的地方。

【讨论】:

    猜你喜欢
    • 2014-03-27
    • 1970-01-01
    • 2013-03-11
    • 2018-07-01
    • 2012-12-07
    • 1970-01-01
    • 2012-05-23
    • 2019-07-29
    • 2015-07-04
    相关资源
    最近更新 更多