1 回答
TA贡献1880条经验 获得超4个赞
我的算法工作正常,它绘制曲线并在更改窗口大小时重新调整大小。
不,它没有。您的绘图永远不会缩放,它保持相同的大小。并且main()设置缩放代码的函数永远不会被调用,因为它遵循mainloop()将控制权移交给 tkinter 事件处理程序的调用:
my_win.mainloop()
main()
使用事件计时器是解决此问题的错误方法。然而,由于 turtle 没有暴露底层的 tkinter 窗口调整大小事件,让我们一起玩这个模型,而不是下降到 tkinter 层。我会这样做:
from turtle import Turtle, Screen
def hilbert_curve(turtle, A, parity, n):
'''
Draw the hilbert curve using recursion.
Arguments:
turtle is for the Turtle,
A is the length of the lines,
parity is for inverting the direction,
and n is for the order
'''
if n < 1:
return
turtle.left(parity * 90)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.forward(A)
turtle.right(parity * 90)
hilbert_curve(turtle, A, parity, n - 1)
turtle.forward(A)
hilbert_curve(turtle, A, parity, n - 1)
turtle.right(parity * 90)
turtle.forward(A)
hilbert_curve(turtle, A, - parity, n - 1)
turtle.left(parity * 90)
def main():
order = 4
parity = 1
length = 100 / (4 * order - 1)
def onResize():
# Rescale drawing when changing window size (the hard way)
turtle.reset()
screen.setworldcoordinates(0, 0, 100, 100)
hilbert_curve(turtle, length, parity, order)
screen.update()
screen.ontimer(onResize, 1000)
screen = Screen()
screen.tracer(False)
turtle = Turtle()
onResize()
screen.mainloop()
main()
即无论窗口大小如何都保持恒定的虚拟坐标,并重新绘制曲线以适应当前窗口大小。顺便说一句,我不是写了这个希尔伯特曲线代码吗?确保从哪里获得它!
添加回答
举报