繁体   English   中英

来自graphics.py的线对象没有绘图

[英]line object from graphics.py not drawing

我正在使用python图形模块(John Zelle)。 我之前从未遇到过任何问题,我可能只是忽略了一些东西,但我找不到它。 我正在尝试绘制一个tic tac toe board。 线条没有插入.Shell说错误是在graphics.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

在对graphics.py的内部机制进行了一些挖掘之后,我注意到你有一些奇怪的东西:

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

这看起来像文档字符串似乎有点奇怪:

将窗口的坐标设置为从左下角的(x1,y1)到右上角的(x2,y2)。

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

上面解释的docstring的图像

因此,您将屏幕定义为从左下角的(8,1)到右上角的(6,1)......等等,什么?

这意味着不仅你的x坐标非常向后,而且线条Line(Point(2,1),Point(2,5))8-6的x范围内8-6

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

我想你想要一个跨度更像:

win.setCoords(0,0,6,7)

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

暂无
暂无

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

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