简体   繁体   English

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

[英]line object from graphics.py not drawing

I'm using python graphics module (John Zelle). 我正在使用python图形模块(John Zelle)。 I have never had a problem before, I might just be overlooking something but I can't find it. 我之前从未遇到过任何问题,我可能只是忽略了一些东西,但我找不到它。 I'm trying to draw a tic tac toe board. 我正在尝试绘制一个tic tac toe board。 The lines are not drawing in. The Shell says the error is in graphics.py but I have redownloaded it from different sources, skimmed it over even, and cannot figure out what I'm doing wrong. 线条没有插入.Shell说错误是在graphics.py中,但是我已经从不同来源重新加载它,甚至掠过它,并且无法弄清楚我做错了什么。 Please help. 请帮忙。


#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

After a bit of digging through the inner mechanics of graphics.py I noticed you have something a bit perculiar: 在对graphics.py的内部机制进行了一些挖掘之后,我注意到你有一些奇怪的东西:

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

This seems a bit odd to me looking at the docstring: 这看起来像文档字符串似乎有点奇怪:

Set coordinates of window to run from (x1,y1) in the lower-left corner to (x2,y2) in the upper-right corner. 将窗口的坐标设置为从左下角的(x1,y1)到右上角的(x2,y2)。

Note that if you are using IDLE you can see this doc string by making win global or doing GraphWin.setCoords( and the docstring should appear on the ( 请注意,如果您使用IDLE,则可以通过使win global或执行GraphWin.setCoords(来查看此doc字符串GraphWin.setCoords(并且docstring应出现在(

上面解释的docstring的图像

So you are defining your screen to span from (8,1) in the bottom left corner to (6,1) in the top right corner... wait, what? 因此,您将屏幕定义为从左下角的(8,1)到右上角的(6,1)......等等,什么?

This means that not only your x coordinates are very backwards, but the line Line(Point(2,1),Point(2,5)) is not even in the x span of 8-6 . 这意味着不仅你的x坐标非常向后,而且线条Line(Point(2,1),Point(2,5))8-6的x范围内8-6

And height of your canvas spans from 1 to 1, as in it has a height of 0 resulting in undefined behaviour. 并且画布的高度从1到1,因为它的高度为0,导致未定义的行为。 (hence the ZeroDivisionError ) (因此ZeroDivisionError

I think you want a span more like: 我想你想要一个跨度更像:

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

This will get stuff properly displaying on your screen, although I expect you to adjust the exact numbers appropriately :) 这将使您的屏幕上显示正确的内容,但我希望您能够适当地调整确切的数字:)

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

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