如何解决如何运行一次函数,然后直到另一个函数运行才运行?
我正在用 python 制作电灯开关。我怎样才能让 turn_off 函数运行一次,然后直到 turn_on 函数(我尚未创建)运行才运行?现在,如果我再次单击按钮,会弹出一个奇怪的方块,所以我想禁用该功能,直到运行 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,p)
p.color("white")
rounded_rectangle(switch,"white")
rounded_rectangle(switch,"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.penup()
win.bgcolor("white")
go_without_draw(-24,p)
p.color("black")
rounded_rectangle(switch,"black")
rounded_rectangle(switch,"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,-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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。