gpt4 book ai didi

python - 无法解释小部件打包和未打包时的行为差异

转载 作者:行者123 更新时间:2023-12-01 07:00:24 24 4
gpt4 key购买 nike

我正在制作一个小应用程序来可视化声音,但控件的行为不一致。当我启用(打包)输入字段时,程序将按我的预期运行。但是,如果我将它们注释掉,情节就不再显示,我只能听到声音。真的很困惑这个......

(在 Ubuntu 18.04 上运行 python 3.7.4)

以下作品:

import threading
import numpy as np
import tkinter as tk
from tkinter import Label, Button, Entry, TOP, BOTH
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
from matplotlib import pyplot as plt
from matplotlib.animation import FuncAnimation
import pyaudio

DEFAULT_FREQUENCY = 420 # frequency in Hz
DEFAULT_DURATION = 3.0 # length of sound stream in seconds
VOLUME = 0.1 # must < 1
INTERVAL = 100 # plot interval in millisecond
PACKAGE_LENGTH = 1024 # number of samples in sound package
FS = 2**12 # sampling frequency sound, normal is 44100


class SoundVisualiser:

def __init__(self, root):
self.root = root
self.root.title("Sound Visualiser")

label = tk.Label(self.root, text="Sound Visualiser")
label.pack()

self.fig, self.ax = plt.subplots(figsize=(5, 5))
self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)
self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
self.ax.set_xlim(1000 * PACKAGE_LENGTH / FS, 0)

freq_label = tk.Label(self.root, text='Frequency')
self.frequency_entry = Entry(self.root)
self.frequency_entry.insert(0, DEFAULT_FREQUENCY)

durat_label = tk.Label(self.root, text='Duration')
self.duration_entry = Entry(self.root)
self.duration_entry.insert(0, DEFAULT_DURATION)

freq_label.pack(side='left')
self.frequency_entry.pack(side='left')
durat_label.pack(side='left')
self.duration_entry.pack(side='left')

self.quit_button = Button(
self.root, text='Quit', command=self.quit)
self.quit_button.pack(side='right')

self.control_button = Button(
self.root, text='Start')
self.control_button.bind('<Button-1>', self.start_visualisation)
self.control_button.pack(side='right')

self.duration = DEFAULT_DURATION
self.xdata = np.linspace(0, 1000 * PACKAGE_LENGTH / FS, PACKAGE_LENGTH)

self.audio = pyaudio.PyAudio()

def quit(self):
self.audio.terminate()
self.root.quit()

def generate_sound_stream(self):
self.sound_stream = (
(0.5 * np.sin(2 * np.pi * 325 / FS *
np.arange(FS * self.duration))) +
(0.1 * np.sin(2 * np.pi * 330 / FS *
np.arange(FS * self.duration))) +
(0.5 * np.sin(2 * np.pi * 340 / FS *
np.arange(FS * self.duration))) + 0
).astype(np.float32)

self.ax.set_ylim(1.1 * np.min(self.sound_stream), 1.1 * np.max(self.sound_stream))

def callback(self, in_data, frame_count, time_info, status):
out = self.sound_stream[:frame_count]
self.out_plot = out[:]
self.sound_stream = self.sound_stream[frame_count:]
return (out*VOLUME, pyaudio.paContinue)

def play_sound(self):
stream = self.audio.open(format=pyaudio.paFloat32,
channels=1,
rate=FS,
output=True,
stream_callback=self.callback)

stream.start_stream()
while stream.is_active():
pass

stream.stop_stream()
stream.close()

self.visualisation = None
self.control_button.config(text='Start')

def update_frame(self, frame):
samples = len(self.out_plot)
if samples == PACKAGE_LENGTH:
self.line.set_data(self.xdata, self.out_plot)

else:
xdata = np.linspace(0, 1000 * samples / FS, samples)
self.line.set_data(xdata, self.out_plot)

return self.line,

def start_visualisation(self, event):
self.duration = float(self.duration_entry.get())
self.generate_sound_stream()

self.line, = self.ax.plot([], [], lw=3)

self.control_button.config(text='run')
duration_range = np.arange(0, self.duration, INTERVAL / 1000)
self.visualisation = FuncAnimation(self.fig,
self.update_frame,
frames=duration_range,
interval=INTERVAL,
repeat=False)

# start audio in a seperate thread as otherwise audio and
# plot will not be at the same time
self.play_sound_thread = threading.Thread(target=self.play_sound)
self.play_sound_thread.start()

def main():
root = tk.Tk()
sound_visualiser = SoundVisualiser(root)
root.mainloop()

if __name__ == '__main__':
main()

注释掉条目字段将停止显示绘图

    def __init__(self, root):
self.root = root
self.root.title("Sound Visualiser")

label = tk.Label(self.root, text="Sound Visualiser")
label.pack()

self.fig, self.ax = plt.subplots(figsize=(5, 5))
self.canvas = FigureCanvasTkAgg(self.fig, master=self.root)
self.canvas.get_tk_widget().pack(side=TOP, fill=BOTH, expand=1)
self.ax.set_xlim(1000 * PACKAGE_LENGTH / FS, 0)

freq_label = tk.Label(self.root, text='Frequency')
self.frequency_entry = Entry(self.root)
self.frequency_entry.insert(0, DEFAULT_FREQUENCY)

durat_label = tk.Label(self.root, text='Duration')
self.duration_entry = Entry(self.root)
self.duration_entry.insert(0, DEFAULT_DURATION)

# freq_label.pack(side='left')
# self.frequency_entry.pack(side='left')
# durat_label.pack(side='left')
# self.duration_entry.pack(side='left')

self.quit_button = Button(
self.root, text='Quit', command=self.quit)
self.quit_button.pack(side='right')

self.control_button = Button(
self.root, text='Start')
self.control_button.bind('<Button-1>', self.start_visualisation)
self.control_button.pack(side='right')

self.duration = DEFAULT_DURATION
self.xdata = np.linspace(0, 1000 * PACKAGE_LENGTH / FS, PACKAGE_LENGTH)

self.audio = pyaudio.PyAudio()

正常输出有声音 enter image description here

最佳答案

我通过添加解决了这个问题

self.root.after(5, self.fig.canvas.draw)

作为函数中的最后一行def start_visualization(self, event)

我添加了一点延迟,因此线程 play_sound 有时间创建其第一个“out_plot”包。

另请参阅how-to-update-a-plot-on-tkinter-canvas?

关于python - 无法解释小部件打包和未打包时的行为差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58662446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com