import turtle
window = turtle.Screen()
pen = turtle.Turtle()
def star():
pen.forward(100);
turtle.onscreenclick(star())
window.mainloop()
I am very new to turtle,and python itself, I am just using some of the documentation that I found on the python website, but when I run this program it runs my function star without the screen even being clicked. The end goal of this program would be for me to click then it would run the function, then if I clicked again the screen would clear and the function would be run again.
Thanks for the help!
You need to pass reference to start function into onscreenclick, and make start to accept two arguments:
def star(x,y):
pen.forward(100);
turtle.onscreenclick(star)
Related
I have the following code:
import turtle
my_pen = turtle.Turtle()
window = turtle.Screen()
window.setup(width=1000, height=1000)
window.title('Tutorial')
my_pen.color("red")
my_pen.penup()
my_pen.goto(0, 0)
window.delay(1000)
my_pen.pendown()
window.delay(100)
my_pen.forward(100)
my_pen.left(90)
my_pen.forward(100)
my_pen.left(90)
turtle.mainloop()
When I try to run the code, the window opens and closes immediately. I am using the newest python 3.9, and I am using the newest PyCharm Community
I will attach a video of me running it and the window closing immediately
Two things: first, I don't know that the delay() method is doing anything for you, it doesn't add anything in my environment, so I'd leave it out until you've debugged the rest of the code.
Second, your code works fine for me. So I suggest you stop looking at the code and look at the environment in which you're running it. You're not simply running Python at the console but rather using some sort of IDE (Idle?). If so, you should include that information in your question.
A simple code clean up for testing purposes:
from turtle import Screen, Turtle
window = Screen()
window.setup(width=1000, height=1000)
window.title('Tutorial')
my_pen = Turtle()
my_pen.color("red")
my_pen.forward(100)
my_pen.left(90)
my_pen.forward(100)
my_pen.left(90)
window.mainloop()
I made a Snake game program and I've made a function called Starting_screen() that opens a window with labels and sprites that shows the starting menu of the game where the player can choose to start a new game, check the leader board, or quit the game.
My main problem is that after the player finishes a game of Snake, I want the program to return to the main menu but it just hangs after the game exits. I've tried putting print statements within the Starting_Screen() function and it actually prints twice, indicating that it re-enters the function but does not open the window again. The flow of the program looks somewhat like this (in simpler sense):
The game is being run by main.py that has:
import interface
import engine
interface.main()
print('outside the interface.main()')
interface.main()
As you can see I've just initially tried to put two interface.main() functions so that I can check if the program runs the second one. It actually prints the statement which indicates it exits the function.
The interface.main() runs only the Starting_screen(), and the Starting_screen() looks like this (simplified):
def Starting_screen():
print('inside starting screen')
window = pyglet.window.Window()
some_text = pyglet.text.Label()
some_image = pyglet.image.load()
#window.event
def on_draw():
window.clear()
some_text.draw()
some_image.draw()
#window.event
def on_key_press(symbol,modifier):
if symbol == key.P:
window.close()
engine.main()
pyglet.app.run()
and the engine.main() just runs the game (using pygame) until the player loses and it will pygame.display.quit() that will terminate the engine.main() function and returns to the Starting_screen() which in turn terminates the function.
I was hoping it would open the main menu window again but it does not. But interestingly enough, it runs print('inside starting screen') twice, indicating that it runs the function again, but it does not open the window like what would normally happen when it was run for the first time.
Any suggestions would be appreciated.
I am trying to draw a square using turtle in python, however, it's not opening any screen.
There are no errors in the shell. I am using python 2.7.14 on windows 10
import turtle
def draw_square(some_turtle):
for i in range(1,5):
some_turtle.forward(100)
some_turtle.right(90)
def draw_art ():
window = turtle.Screen()
window.bgcolor("green")
fahim = turtle.Turtle()
fahim.shape("turtle")
fahim.color("yellow")
fahim.speed(2)
for i in range(1,37):
draw_square(fahim)
fahim.right(10)
window.exitonclick()
You've defined the functions, but you haven't called them.
Functions only run when they're called, and as you haven't called them, no animation will be displayed. This means that you need to call your functions o get the animation.
Just add draw_Art() to the end of your code and there should be an animation.
I have some code as follows:
# My code here
turtle.bye()
After that, is there any way I can reopen the turtle window.
I know you can do turtle.clearscreen() but that does not close the turtle window.
I will accept any answer which allows me to close the turtle graphics window and then reopen it without opening and running another python program to do this.
Thank you in advance
I've seen situations where the approach of #LukeTimmons worked but not always reliably and not in every situation. Give this solution a try:
import time
import turtle
turtle.dot(200, 'green')
time.sleep(2)
turtle.bye()
# These two lines (indirectly) resurrect turtle environment after turtle.bye()
turtle.Turtle._screen = None # force recreation of singleton Screen object
turtle.TurtleScreen._RUNNING = True # only set upon TurtleScreen() definition
turtle.dot(200, 'red')
turtle.mainloop()
It resets two flags that keep turtle from starting up again. It may be safer to create your own turtle after restart rather than use the default turtle which may point back to the departed environment.
There may be other ways but this is the only way I know.
from turtle import *
def turtle1():
#Your code here
turtle1()
turtle.bye()
turtle1()
This should re-run your code without re-typing it.
The following code creates a window in which a python turtle follows wherever your mouse goes. However, it draws indescriminately of whether the user is clicking to draw or not. My code is the following:
Note: You must have pythonwin installed in order for this program to work properly
import turtle, win32api
while True:
# turtle.penup()
user = win32api.GetCursorPos()
mousepos = [user[0]-510,-1*(user[1])+ 410]
turtle.goto(mousepos)
turtle.onclick(turtle.pendown())
In theory, this would only draw when the user is pressing and holding the mouse, but it doesn't work in practice. The commented code will simply cause it to not draw at all. Any advice?
turtle.onclick(turtle.pendown())
Here, you are calling pendown(), which returns probably None; then you're passing this None to onclick(). It probably means "do nothing on a click". That's probably not what you want.
According to #Gibby's comment, you want:
def clicked(*args): # args ignored
turtle.pendown()
turtle.onclick(clicked)