我在 python2-7 我想在 tkinter 中获得一个按钮,它停止阅读用流体合成器创建的笔记。
我发现常见的解决方案是使用 time.after 像这里:你如何创建 Tkinter GUI 停止按钮来打破无限循环?
但在我的情况下,我不能使用它,因为我需要在 noteon 和 noteoff 之间有一段时间来为我的笔记提供持续时间。此外,我只想在单击开始时播放音符(而不是像链接中的解决方案一样在开头)。
所以我创建了这段代码,但它不起作用,因为 var_start 总是初始化为 int:
from tkinter import*
import fluidsynth
import time
fs=fluidsynth.Synth()
fs.start(driver='alsa', midi_driver='alsa_seq')
org_charge = fs.sfload("organ.sf2")
fs.program_select(0,org_charge, 0, 0)
time.sleep(1)
var_start=int
def start():
global var_start
var_start=1
def stop():
global var_start
var_start=0
root=Tk()
if var_start==1:
fs.noteon(0,67,127)
time.sleep(1)
fs.noteoff(0,67)
fs.noteon(0,71,127)
time.sleep(1)
fs.noteoff(0,71)
fs.noteon(0,74,127)
time.sleep(1)
fs.noteoff(0,74)
Button(root, text='start', command= start).pack(padx=10, pady=10)
Button(root, text='stop', command= stop).pack(padx=10, pady=10)
root.mainloop()
我没有其他想法来重塑我的代码......有人可以帮助我吗?
谢谢