0

另一个超级绿色的问题,我敢肯定:

我对节拍器/音序器的热情一直在进步,并且有一点背景知识,我知道我会将这段代码放入 Raspberry Pi 并在某种屏幕上显示信息,所以我编写了这段代码并将海龟导入为了有一天在 320 x 240 的屏幕上显示它。

我有一个循环计算小节和节拍,并将其显示在一个尺寸为 320 x 240 的海龟窗口中。

我希望能够按键盘上的右箭头启动节拍器,按键盘上的左箭头停止它。

我尝试了各种版本的 onkey()、onkeyrelease() 和 onkeypress(),但不知道如何中断循环以停止它或让它等待命令启动它。

下面的代码:

import os
import time
import turtle
from turtle import *

win = turtle.Screen()
win.title("Beats & Measures")
win.bgcolor("black")
win.setup(width=240, height=320)
win.tracer(0)

pen1 = turtle.Turtle()
pen1.speed(0)
pen1.color("white")
pen1.penup()
pen1.hideturtle()
pen1.goto(150,-120)

pen2 = turtle.Turtle()
pen2.speed(0)
pen2.color("white")
pen2.penup()
pen2.hideturtle()
pen2.goto(160,-70)

pen3 = turtle.Turtle()
pen3.speed(0)
pen3.color("white")
pen3.penup()
pen3.hideturtle()
pen3.goto(-160,-70)

pen4 = turtle.Turtle()
pen4.speed(0)
pen4.color("white")
pen4.penup()
pen4.hideturtle()
pen4.goto(-160,-140)

three4 = range(1,4)
four4 = range(1,5)
six8 = range(1,7)
eighth = range(1,9)
twelfth = range(1,13)
sixteenth = range(1,17)
thirty2nd = range(1,33)

signatures = [three4,four4,six8,eighth,twelfth,sixteenth,thirty2nd]

beat = signatures[1]



pen3.write("Time Signature", align="left", font=('Digital-7 Mono', 18, 'bold'))
# pen4.write("Choose Time Signature: ", align="left", font=('Digital-7 Mono', 14, 'bold'))


def beatsMeasures():
    measure = 1
    while True:
        for i in beat:
            time.sleep(.5)
            pen2.write("measure : beat",align="right", font=('Digital-7 Mono', 18, 'bold'))
            pen1.clear()
            pen1.write("{}:{}".format(measure, i), align="right", font=('Digital-7 Mono', 48, 'normal'))
        measure = measure + 1


beatsMeasures()
4

1 回答 1

0

您可能不会成功while True:time.sleep()嵌入到像turtle 这样的事件驱动环境中。让我们把你的程序拆开并使用turtle的定时器事件重建它:

from turtle import Screen, Turtle
from itertools import cycle, count

def nextBeat(beat, measure):
    pen1.clear()
    pen1.write("{}:{}".format(measure, beat), align='right', font=('Arial', 48, 'normal'))

    beat = next(beats)

    if beat == 1:
        if not pause:
            screen.ontimer(nextMeasure, 500)
    else:
        screen.ontimer(lambda: nextBeat(beat, measure), 500)

def nextMeasure():
    measure = next(measures)

    pen2.write("measure : beat", align='right', font=('Arial', 18, 'bold'))

    nextBeat(1, measure)

pause = False

def start():
    global pause

    pause = False

    nextMeasure()

def stop():
    global pause

    pause = True

screen = Screen()
screen.title("Beats & Measures")
screen.bgcolor('black')
screen.setup(width=400, height=300)
screen.tracer(0)

pen1 = Turtle()
pen1.hideturtle()
pen1.color('white')
pen1.penup()
pen1.goto(150, -120)

pen2 = pen1.clone()
pen2.goto(160, -70)

pen3 = pen2.clone()
pen3.goto(-160, -70)

three4 = range(1, 4)
four4 = range(1, 5)
six8 = range(1, 7)
eighth = range(1, 9)
twelfth = range(1, 13)
sixteenth = range(1, 17)
thirty2nd = range(1, 33)

signatures = [three4, four4, six8, eighth, twelfth, sixteenth, thirty2nd]

beats = cycle(signatures[1])

measures = count(1)

pen3.write("Time Signature", align='left', font=('Arial', 18, 'bold'))

screen.onkey(start, 'Right')
screen.onkey(stop, 'Left')
screen.listen()

nextMeasure()

screen.mainloop()

按左箭头键将停止程序,但在我的简单实现中,它将继续跳动到小节结束,然后停止。按下右箭头键将在下一小节开始时恢复。可能不是您想要的,但希望是您需要的起点。

于 2020-04-19T07:09:16.343 回答