Python tkinter 未完成绘图

标签 python tkinter

我必须用 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/

相关文章:

python - 查看 Pandas 中两列是否一对一的简单方法

python - 我想用 python 抓取一个印地语(印度语言)pdf 文件

python - 在两个数组中查找重复值,Python

python - Python 中 tkinter 中的协议(protocol)

python - 如何在返回列表项的标签中显示函数的结果?

python - 使用 python Tkinter 在图像上创建文本

python - 错误 : At line 463 of file custom. f90:内存分配失败

python - 在 Python Google App Engine 中,如何模拟或子类化 File 类,以便编写用于访问文件的软件不会抛出异常?

python - Virtualbox Ubuntu pygame.display.set_mode 在交互式 python 中的行为与解释文件时的行为不同

python - 在循环中为 Tkinter 条目小部件创建 StringVar 变量