【问题标题】:My Tkinter won't allow me to click the button that will show the sine wave我的 Tkinter 不允许我单击显示正弦波的按钮
【发布时间】: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


【解决方案1】:

所以我发现了一个与我要问的问题非常相似的问题,我想我应该定义一个“PlotWindow”,就像一个具有数学运算的窗口,并且可以传输要显示的数据......如果有人可以让我了解该功能的工作原理,或者大致了解它的外观会有很大帮助,我认为我对此不太熟悉。感谢所有回复我原帖的人。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-10
    • 1970-01-01
    • 1970-01-01
    • 2021-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多