线条对象来自图形.py不是画画

2024-04-26 18:34:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用的是python图形模块(johnzelle)。我以前从来没有遇到过问题,我可能只是忽略了一些东西,但我找不到它。我试着画一个图板。线没有画进去。外壳说错误在图形.py但我已经从不同的来源重新下载了它,甚至浏览了一遍,却不知道我做错了什么。请帮忙。在


#here is my code sample
#import graphics library
from graphics import *
#build interface
def interface():
    win = GraphWin("Tic Tac Toe", 600,700)
    win.setCoords(8,1,6,1)
    #horizontal line #1
    h1 = Line(Point(2,1),Point(2,5))
    h1.draw(win)
#...there is more but it's repetitive so I won't waste time.

>>> interface()
Traceback (most recent call last):
  File ".../test.py", line 12, in <module>
    interface()
  File ".../test.py", line 10, in interface
    h1.draw(win)
  File "/LIB/graphics.py", line 450, in draw
    self.id = self._draw(graphwin, self.config)
  File "/LIB/graphics.py", line 627, in _draw
    x1,y1 = canvas.toScreen(p1.x,p1.y)
  File "/LIB/graphics.py", line 335, in toScreen
    return self.trans.screen(x,y)
  File "/LIB/graphics.py", line 386, in screen
    ys = (self.ybase-y) / self.yscale
ZeroDivisionError: float division by zero

Tags: inpyimportself图形islibline
1条回答
网友
1楼 · 发布于 2024-04-26 18:34:36

在深入研究了graphics.py的内在机制之后,我注意到你有一点感性:

win.setCoords(8,1,6,1)

这在我看来有点奇怪,看看docstring:

Set coordinates of window to run from (x1,y1) in the lower-left corner to (x2,y2) in the upper-right corner.

注意,如果您使用的是IDLE,则可以通过使win全局或执行GraphWin.setCoords(来查看此doc字符串,并且docstring应该出现在(

image of the docstring explained above

所以你要定义你的屏幕从左下角的(8,1)到右上角的(6,1)。。。等等,什么?在

这意味着不仅你的x坐标非常向后,而且Line(Point(2,1),Point(2,5))线甚至不在{}的x跨度内。在

画布的高度从1到1,因为它的高度0会导致未定义的行为。(因此ZeroDivisionError

我想你想要的跨度更像:

^{pr2}$

这将使内容正确显示在您的屏幕上,尽管我希望您适当地调整确切的数字:)

相关问题 更多 >