I am making a light switch in python. How can I make it so that the turn_off function runs once, and then doesn't run until the turn_on function (which I have yet to create) runs? Right now, If I click the button again, a weird square pops up, so I would lik to disable the function until the turn_on function is run. Here is my code (edited):
import turtle
win = turtle.Screen()
win.setup(width=1.0,height=1.0,startx=None,starty=None)
switch = turtle.Turtle(visible=False)
def go_without_draw(x,y,t):
t.penup()
t.goto(x,y)
t.pendown()
def shape(sides):
for i in range(sides):
angle = 360/sides
length = 50
t.forward(length)
t.left(angle)
def rounded_rectangle(turtle, short, long, radius,color):
turtle.color(color)
diameter = radius * 2
heading = turtle.heading()
turtle.setheading(270)
isdown = turtle.isdown()
if isdown:
turtle.penup()
turtle.goto(turtle.xcor() - long/2, turtle.ycor() - short/2 + radius)
turtle.pendown()
for _ in range(2):
turtle.circle(radius, 90)
turtle.forward(long - diameter)
turtle.circle(radius, 90)
turtle.forward(short - diameter)
turtle.penup()
turtle.goto(turtle.xcor() + long/2, turtle.ycor() + short/2 - radius)
if isdown:
turtle.pendown()
turtle.setheading(heading)
p = turtle.Turtle(visible=False)
p.penup()
p.goto(-24,0)
p.pendown()
p.goto(24,0)
p.penup()
def gotoandprint(x, y):
t = turtle.Turtle(visible=False)
p.penup()
gotoresult = t.goto(x, y)
print(t.xcor(), t.ycor())
return gotoresult
win.onclick(gotoandprint, 3)
rounded_rectangle(switch,150,100,25,"black")
rounded_rectangle(switch,75,50,12.5,"black")
def turn_off():
p.penup()
p.goto(-24,0)
p.pendown()
p.goto(24,0)
p.penup()
win.bgcolor("black")
go_without_draw(-24,0,p)
p.color("white")
rounded_rectangle(switch,150,100,25,"white")
rounded_rectangle(switch,75,50,12.5,"white")
p.begin_fill()
p.forward(48)
p.left(90)
p.forward(30)
p.left(30)
p.forward(8)
p.left(60)
p.forward(35)
p.left(30)
p.forward(8)
p.left(25)
p.end_fill()
def turn_on():
p.penup()
p.goto(-24,0)
p.pendown()
p.goto(24,0)
p.penup()
win.bgcolor("white")
go_without_draw(-24,0,p)
p.color("black")
rounded_rectangle(switch,150,100,25,"black")
rounded_rectangle(switch,75,50,12.5,"black")
p.begin_fill()
p.backward(48)
p.right(90)
p.backward(30)
p.right(30)
p.backward(8)
p.right(60)
p.backward(35)
p.right(30)
p.backward(8)
p.right(25)
p.end_fill()
p.onclick(turn_off)
b = turtle.Turtle(visible=False)
flag = False
def go(x,y):
global flag
b.penup()
result = b.goto(x,y)
b.pendown()
x_coord = b.xcor()
y_coord = b.ycor()
print(x_coord,y_coord)
if x_coord in range(-24,24) and y_coord in range(-1,33) and flag == True:
turn_off()
flag = False
p.penup()
p.goto(-24,0)
p.pendown()
if x_coord in range(-24,24) and y_coord in range(-1,-33) and flag == False:
print('This code runs')
turn_on()
flag == True
p.penup()
p.goto(-24,0)
p.pendown()
win.onscreenclick(go)
win._root.mainloop()
You could use a boolean
flag variable, like so:
flag = True
def func1():
global flag
if flag: # if self.flag == True:
print("I am Function1 !!!")
flag = False # This makes it so that the next time your function runs, it won't do anything because flag is false
def func2():
global flag
... # Do whatever
flag = True # Our flag is now True, meaning that fun1 will run as executed
func1() # Should print
func1() # Should not print
func2() # Should not do anything
func1() # Should print