我必须用 tkinter 绘制圣诞老人的房子,但是当使用以下代码时,它会在第一行之后停止
首先我导入海龟并打开 GUI 屏幕
import turtle as t
t.Screen()
然后我使用这个代码
>>> s = 100
>>> points = [(s,0), (s,s), (0,0), (s,s), (s/2.,2.*s), (0,s), (s,0)]
>>> for p in points:
t.goto(p)
t.mainloop()
但它在第一行之后停止,我该如何解决这个问题?
最佳答案
正如turtle.mainloop
文档中所指定的那样:
turtle.mainloop()
Starts event loop - calling Tkinter’s mainloop function. Must be the last statement in a turtle graphics program. Must not be used if a script is run from within IDLE in -n mode (No subprocess) - for interactive use of turtle graphics
但是,这不是最后一个语句:它是为 points
中的每个 point
调用的。所以你必须重写你的程序:
for p in points:
t.goto(p)
t.mainloop()
如果您阅读了规范的第二个粗体部分,甚至没有必要调用它:您的程序不是交互式的。所以下面的程序就足够了:
import turtle as t
t.Screen()
for p in points:
t.goto(p)
关于Python tkinter 未完成绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41594523/