Python 特金特音阶和动画奖';跑不动

Python 特金特音阶和动画奖';跑不动,python,matplotlib,animation,tkinter,tkinter-scale,Python,Matplotlib,Animation,Tkinter,Tkinter Scale,所以我试图让我的代码让用户定义频率和持续时间。在那之后,我的程序获取频率并用pyaudio播放,我想要一个动画出现。我设法做到了这一点,但我的问题是,当我放入一个Tkinter刻度,使延迟类似于多普勒效应。我尝试将DoubleVar设置为字母d表示的字符串,但我的程序在发出声音后刚刚退出 p=pyaudio.PyAudio() fs=44100 volume=.7 f=int(input('Enter your frequency:')) t = float(input('Enter durat

所以我试图让我的代码让用户定义频率和持续时间。在那之后,我的程序获取频率并用pyaudio播放,我想要一个动画出现。我设法做到了这一点,但我的问题是,当我放入一个Tkinter刻度,使延迟类似于多普勒效应。我尝试将DoubleVar设置为字母d表示的字符串,但我的程序在发出声音后刚刚退出

p=pyaudio.PyAudio()
fs=44100
volume=.7
f=int(input('Enter your frequency:'))
t = float(input('Enter duration of signal (seconds):'))
samples=(np.sin(f*2*np.pi*np.arange(fs*t)/(fs)).astype(np.float32))
stream=p.open(format=pyaudio.paFloat32,
            channels=1,
            rate=fs,
            output=True)

stream.write(volume*samples)
stream.stop_stream()
stream.close()
p.terminate()

class Example(tk.Tk):
    def __init__(self):
        super().__init__()
        self.dou_var=tk.DoubleVar()
        self.scale = tk.Scale(self, from_=1, to=100, resolution=0.01,
                              variable=self.dou_var)
        self.scale.pack()
        tk.Button(self, text="Check Scale", command=self.check_scale).pack()

    def check_scale(self):
            d=str(self.dou_var.get())

def main():
    if __name__ == "__main__":
        Example().after()

fig = plt.figure()
ax = plt.axes(xlim=(0, 2), ylim=(-2, 2))
line = ax.plot([], [], lw=2)

def init():
    line.set_data([], [])
    return line,
def animate(i):
    x = np.linspace(0, 2, 1000)
    y = np.sin(np.sin((f/d)*2*np.pi*np.arange(fs*t)/(fs)).astype(np.float32)*i)
    line.set_data(x, y)
    return line,
anim = animation.FuncAnimation(fig, animate, init_func=init,
                               frames=100, interval=20, blit=True)

没有错误消息。

您导入了正确的模块吗?@SF12 Study是的,我使用了matplotlib、numpy和tkinter,但我没有将其添加到原始帖子中,以防添加到此处:将numpy作为np导入,从matplotlib将pyplot作为plt导入,将pyaudio导入,从numpy导入tkinter作为tk从matplotlib.backends.backend导入arange、sin、pi从matplotlib.backend\u tkagg从matplotlib.figure导入figure从tkinter导入figure导入ttk导入numpy作为np导入matplotlib.animation作为animation‘我不知道在
类中是否需要它,
,但是看起来您没有添加
Example.mainloop()