我想,如果用户连续调用两个函数,则两个函数之间会出现某种类型的“延迟”,因此在执行第一个函数时不会中断按键。也许像队列这样的东西会做。为了想象我正在谈论的内容,这里是我想要调用的函数,根据按键,在乌龟图形窗口中绘制相应的字母:
def draw_H():
# Draw the left leg of H.
# The turtle starts at the bottom left of the letter, pointing right.
left(90)
forward(letter_height)
# Draw the bar of the H.
# The turtle starts at the top of the left leg, pointing up.
forward(-letter_height/2)
right(90)
forward(letter_width)
# Draw the right leg of the H.
# The turtle starts at the right side of the bar, pointing right.
left(90)
forward(letter_height/2)
forward(-letter_height)
right(90)
# The H is drawn.
# The turtle is in the top right, pointing right.
draw_space()
def draw_E():
# Draw an E.
left(90)
forward(letter_height)
right(90)
forward(letter_width)
forward(-letter_width)
right(90)
forward(letter_height/2)
left(90)
forward(letter_width)
forward(-letter_width)
right(90)
forward(letter_height/2)
left(90)
forward(letter_width)
draw_space()
def draw_L():
# Draw an L
left(90)
forward(letter_height)
forward(-letter_height)
right(90)
forward(letter_width)
draw_space()
def draw_O():
# Draw an O
forward(letter_width)
left(90)
forward(letter_height)
left(90)
forward(letter_width)
left(90)
forward(letter_height)
left(90)
forward(letter_width)
draw_space()
def draw_W():
# This function will draw a W
left(105)
forward(letter_height)
backward(letter_height)
right(40)
forward(letter_height/2)
right(131)
forward(letter_height/2)
left(141)
forward(letter_height)
right(165)
penup()
forward(letter_height)
left(90)
draw_space()
def draw_R(letter_width, letter_height):
# This function will draw an R
slant_height = (math.sqrt(letter_width**2 + (letter_height/2)**2))
slant_angle = (90+(90-(math.degrees(math.acos(letter_width/slant_height)))))
space_angle = (180 - slant_angle)
left(90)
forward(letter_height)
right(90)
forward(letter_width)
right(90)
forward(letter_height/2)
right(90)
forward(letter_width)
left(slant_angle)
forward(slant_height)
left(space_angle)
draw_space()
def draw_D(letter_width, letter_height):
# This function will draw a REAL D
angle_height = math.sqrt(letter_width**2 + (letter_height/2)**2)
D_angle = (90+(math.degrees(math.acos(letter_width/angle_height))))
Second_D_angle = ((90 - (D_angle-90)) + (90-(math.degrees(math.acos(letter_width/angle_height)))))
D_space_angle = (math.degrees(math.atan(letter_width/(letter_height/2))))
left(90)
forward(letter_height)
right(D_angle)
forward(angle_height)
right(Second_D_angle)
forward(angle_height)
left(90+D_space_angle)
penup()
forward(letter_width)
draw_space()
以下是调用函数绘制相应字母的方法:
onkey(draw_H, "h")
onkey(draw_E, "e")
onkey(draw_L, "l")
onkey(draw_O, "o")
onkey(draw_W, "w")
onkey(lambda: draw_R(letter_width, letter_height), "r")
onkey(lambda: draw_D(letter_width, letter_height), "d")
现在发生的事情是,当调用该函数并且用户反复按下/按下相同的键到相应的函数时,会发生以下情况:
^当我按住键叫H时,会发生这种情况。另外:
^当我快速连续输入R和H时会发生这种情况。我希望在调用这些函数之间以及激活这些函数之间会稍有延迟,因此每个函数都能够完成它的过程,并且乌龟不会在整个地方疯狂。非常感谢有关此问题的任何帮助! :)
答案 0 :(得分:3)
欢迎来到活动编程世界!
让我们暂时忘掉海龟。你想要的是你的程序在键盘上敲击一些字符时绘图。但是当你画一些东西时,你不希望另一个角色的击中开始一个新的绘图。那么如果一个人处于活动状态,你希望它不要进入新的绘图程序。你只需要一点同步。
因为所有都发生在同一个线程中(无论如何,如果不同的线程试图访问屏幕,大多数GUI都表现不好),实际上你应该使用一个队列来保存绘图请求,如果你已经绘制了这个请求就排队:
q = Queue.Queue()
drawing = False
drawingLock = threading.Lock()
def draw(x):
global drawing
q.put(x)
process = False:
drawingLock.acquire()
if not drawing:
process = True
drawing = True
drawingLock.release()
if process:
while not q.empty():
do_draw(q.get()) # call the actual drawings here
drawingLock.acquire()
drawing = False
drawingLock.release()
应该从onclick事件和调度图调用此draw
函数到您的实际绘图函数。