似乎当turtle
被导入它会创建许多对象并且exitonclick()
删除所有它们 - 不仅Screen()
. exitonclick()
被创建来结束程序。
但你可以使用oscreenclick(function_name)
将功能分配给鼠标单击,这将清除屏幕并绘制下一个对象。onscreenclick
使用两个参数执行函数 - 单击的位置 - 因此函数必须接收此信息。
import turtle
# --- functions ---
def second(x, y):
# clear screen
tod.reset()
tod.color("red", "green")
tod.begin_fill()
for i in range(6):
tod.forward(50)
tod.left(360 / 6)
tod.end_fill()
# run another function on click
#turtle.onscreenclick(third)
# end program on click
turtle.exitonclick()
# --- main ---
tod = turtle.Turtle()
tod.color("red", "green")
tod.begin_fill()
for i in range(3):
tod.forward(50)
tod.left(360 / 3)
tod.end_fill()
# assign function to click on screen
turtle.onscreenclick(second)
# you need it to - it checks if you clicked (and does othere things)
turtle.mainloop()
EDIT:如果您必须删除窗口并再次显示,则可以使用tod._screen._root
访问主窗口,使用tkinter
,您可以隐藏/显示它
tod._screen._root.iconify() # hide
input("Press Enter: ")
tod._screen._root.deiconify() # show again
工作示例:
#!/usr/bin/env python3
import turtle
# --- functions ---
def stop(callback):
#tod._screen._root.attributes("-topmost", False)
tod._screen._root.iconify()
# upper Y in text means that it will be default answer if you press only Enter
answer = input("Show more images? [Y/n]: ").strip().lower()
if not answer: # empty string treat as `Y`
answer = 'y'
tod._screen._root.deiconify()
# problem with moving window above other windows
#tod._screen._root.lift()
tod._screen._root.attributes("-topmost", True)
#tod._screen._root.update()
if answer == 'y':
callback()
else:
#turtle.exitonclick()
# or
turtle.bye()
def first(x=0, y=0):
tod.color("red", "green")
tod.begin_fill()
for i in range(3):
tod.forward(50)
tod.left(360 / 3)
tod.end_fill()
# assign function to click on screen
turtle.onscreenclick(lambda x,y:stop(second))
def second(x=0, y=0):
# clear screen
tod.reset()
tod.color("red", "green")
tod.begin_fill()
for i in range(6):
tod.forward(50)
tod.left(360 / 6)
tod.end_fill()
# assign function to click on screen
turtle.onscreenclick(lambda x,y:stop(third))
def third(x=0, y=0):
# clear screen
tod.reset()
tod.color("red", "green")
tod.begin_fill()
for i in range(12):
tod.forward(50)
tod.left(360 / 12)
tod.end_fill()
# end program on click
turtle.exitonclick()
# --- main ---
tod = turtle.Turtle()
first()
# you need it to - it checks if you clicked (and does othere things)
turtle.mainloop()
但不是input()
您可以使用tkinter
及其消息框
answer = tkinter.messagebox.askyesno('More?', "Show more images?")
工作示例:
#!/usr/bin/env python3
import turtle
import tkinter.messagebox
# --- functions ---
def stop(callback):
answer = tkinter.messagebox.askyesno('More?', "Show more images?")
print('answer:', answer)
if answer:
callback()
else:
#turtle.exitonclick()
# or
turtle.bye()
def first(x=0, y=0):
tod.color("red", "green")
tod.begin_fill()
for i in range(3):
tod.forward(50)
tod.left(360 / 3)
tod.end_fill()
# assign function to click on screen
turtle.onscreenclick(lambda x,y:stop(second))
def second(x=0, y=0):
# clear screen
tod.reset()
tod.color("red", "green")
tod.begin_fill()
for i in range(6):
tod.forward(50)
tod.left(360 / 6)
tod.end_fill()
# assign function to click on screen
turtle.onscreenclick(lambda x,y:stop(third))
def third(x=0, y=0):
# clear screen
tod.reset()
tod.color("red", "green")
tod.begin_fill()
for i in range(12):
tod.forward(50)
tod.left(360 / 12)
tod.end_fill()
# end program on click
turtle.exitonclick()
# --- main ---
tod = turtle.Turtle()
first()
# you need it to - it checks if you clicked (and does othere things)
turtle.mainloop()
EDIT:我检查了源代码turtle
看来你可以设置
turtle.TurtleScreen._RUNNING = True
to run turtle
再次之后exitonclick()
尝试使用和不使用此代码turtle.TurtleScreen._RUNNING = True
import turtle
turtle.goto(0,50)
turtle.exitonclick()
turtle.TurtleScreen._RUNNING = True
turtle.goto(50,150)
turtle.exitonclick()
turtle.TurtleScreen._RUNNING = True
但也许对于更复杂的代码它不会工作,因为exitonclick()
做其他事情 - 执行的原始函数exitonclick()
def _destroy(self):
root = self._root
if root is _Screen._root:
Turtle._pen = None
Turtle._screen = None
_Screen._root = None
_Screen._canvas = None
TurtleScreen._RUNNING = False
root.destroy()