【发布时间】:2019-10-01 03:21:10
【问题描述】:
此代码不起作用,我不知道为什么。我对编程很陌生,并且正在使用 Mac,所以我不太了解。我读到它可能是 matplotlib 的后端,但所有其他文章都没有定论。我想在 GUI 中显示波形,并希望能够使用新数据对其进行更新。
我试过安装和卸载matplotlib,下载Quartz11
import tkinter as tk
from tkinter import Frame, Label, Entry, Button
import pyaudio
from matplotlib.figure import Figure
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import matplotlib. animation as animation
def insert_number():
f=int(entry1.get())
t=float(entry2.get())
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)
fig, ax = plt.subplots()
def dopp():
x = np.arange(0, 2*np.pi, 0.01)
line, = ax.plot(x, np.sin((x)/10))
def animate(i):
line.set_ydata(np.sin(f(x + i/10.0)))
return line,
def init():
line.set_ydata(np.ma.array(x, mask=True))
return line,
ani = animation.FuncAnimation(fig, animate, np.arange(1, 200), init_func=init,
interval=25, blit=True)
plt.show()
canvas = FigureCanvasTkAgg(fig, master=self.window)
canvas.get_tk_widget().pack()
canvas.draw()
start= mclass (window)
window.mainloop()
tk.mainloop()
button2 = tk.Button (root, text='Click to see Waves ',command=insert_number)
canvas1.create_window(97, 270, window=button2)`
没有错误消息,但我知道出了点问题。我很感激任何帮助。谢谢!
【问题讨论】:
-
你能展示你的进口吗?这看起来不像完整的代码。
-
对不起!刚刚添加它们。
-
您正在创建您的按钮在调用
.mainloop(),直到窗口关闭(此时您不能 创建一个按钮)。 -
如果我将按钮移到此部分“开始 = mclass(窗口)...”之前的按钮之前,它告诉我 self 未定义。
标签: python-3.x matplotlib tkinter-canvas