""" 按住某键.py """ import time import turtle WIDTH = 800 HEIGHT = 600 seconds = 0 def text(text, posx, posy, size): turtle.pencolor('black') turtle.goto(posx, posy) ft = ("Arial", size, "normal") turtle.write(text, align='center',font=ft) def press_space(): global seconds seconds = time.time() turtle.onkeypress(None, ' ') def release_space(): if time.time() - seconds >= 3.0: turtle.onkeyrelease(None, ' ') text("thank you.", 0, 0, 15) # 屏幕设定 turtle.screensize(WIDTH, HEIGHT) turtle.title("按住某键测试") turtle.hideturtle() turtle.penup() # 在屏幕上写文本 text('请按住空格键3秒', 0, 200, 15) # 事件处理 turtle.listen() turtle.onkeypress(press_space, ' ') turtle.onkeyrelease(release_space, ' ') # End turtle.done()
李兴球
李兴球的博客是Python创意编程原创博客