如何 运行 函数一次,然后不 运行 直到另一个函数 运行s?

How to run function once, then not run until another function runs?

我正在 python 中制作电灯开关。我怎样才能做到 turn_off 函数 运行 一次,然后 运行 直到 turn_on 函数(我还没有创建)运行s?现在,如果我再次单击该按钮,会弹出一个奇怪的方块,所以我想禁用该功能,直到 turn_on 功能为 运行。 这是我的代码(已编辑):

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()



您可以使用 boolean 标志变量,如下所示:

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