Using Turtle Module Exitonclick()
Solution 1:
It seems that when turtle
is imported it creates many objects and exitonclick()
removes all of them - not only Screen()
. exitonclick()
was created to end program.
But you can use oscreenclick(function_name)
to assign function to mouse click which will clear screen and draw next object. onscreenclick
execute function with two arguments - position of click - so function has to receive this information.
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: if you have to remove window and show again than you can use tod._screen._root
to get access to main window which use tkinter
, and you can hide/show it
tod._screen._root.iconify() # hideinput("Press Enter: ")
tod._screen._root.deiconify() # show again
Working example:
#!/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()
But instead of input()
you can use tkinter
and its messageboxes
answer = tkinter.messagebox.askyesno('More?', "Show more images?")
Working example:
#!/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: I checked source code of turtle
and it seems that you can set
turtle.TurtleScreen._RUNNING = True
to run turtle
again after exitonclick()
Try this code with and without 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
But maybe with more complex code it will not work because exitonclick()
does other things - oryginal function which is executed by 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()
Solution 2:
I have just added one line to your code so that the turtle class gets reimported and in the next initialization the class variable turtle.TurtleScreen._RUNNING will be set to true.
Working Code:
import importlib
import turtle
ws = turtle.Screen()
tod_1 = turtle.Turtle()
tod_1.color("red", "green")
tod_1.begin_fill()
for i inrange(3):
tod_1.forward(50)
tod_1.left(360 / 3)
tod_1.end_fill()
ws.exitonclick()
importlib.reload(turtle)
input("go'press any thing' ")
ws = turtle.Screen()
tod_2 = turtle.Turtle()
tod_2.color("red", "green")
tod_2.begin_fill()
for i inrange(6):
tod_2.forward(50)
tod_2.left(360 / 6)
tod_2.end_fill()
ws.exitonclick()
Post a Comment for "Using Turtle Module Exitonclick()"