Tkinter开始/停止按钮,用于以Python录制音频 [英] Tkinter start/stop button for recording audio in Python

查看:131
本文介绍了Tkinter开始/停止按钮,用于以Python录制音频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个使用Tkinter GUI录制音频的程序.为了录制音频本身,我使用以下代码: https://gist.github.com/sloria/5693955 处于非阻止模式.

I am writing a program to record audio with the use of a Tkinter GUI. For recording audio itself, I use this code: https://gist.github.com/sloria/5693955 in nonblocking mode.

现在,我想实现诸如开始/停止"按钮之类的功能,但感觉好像我错过了某些功能.假设如下:

Now I want to implement something like a start/stop button, but feel like I am missing out on something. Following suppositions:

  1. 我不能在True中使用声明函数,因为它将破坏Tkinter mainloop()

  1. I can't use a while Truestatement either a time.sleep()function because it's going to break the Tkinter mainloop()

结果,我可能必须使用全局 bool 来检查我的 start_recording()函数是否正在运行

As a result, I will probably have to use a global bool to check whether my start_recording()function is running

我将不得不使用与 start_recording 相同的功能调用 stop_recording ,因为两者都必须使用同一对象

I will have to call stop_recording in the same function as start_recordingbecause both have to use the same object

我不能使用 root.after()调用,因为我希望记录是用户定义的.

I can not use root.after() call because I want the recording to be user-defined.

找到以下问题的代码段:

Find a code snippet of the problem below:

import tkinter as tk
from tkinter import Button
import recorder

running = False

button_rec = Button(self, text='Aufnehmen', command=self.record)
button_rec.pack()

button_stop = Button(self, text='Stop', command=self.stop)
self.button_stop.pack()

rec = recorder.Recorder(channels=2)

def stop(self):
    self.running = False

def record(self):
    running = True
    if running:
        with self.rec.open('nonblocking.wav', 'wb') as file:
            file.start_recording()
            if self.running == False:
                file.stop_recording()

root = tk.Tk()
root.mainloop()

我知道某处必须有一个循环,但是我不知道在哪里(以及如何).

I know there has to be a loop somewhere, but I don't know where (and how).

推荐答案

我将使用普通

running = rec.open('nonblocking.wav', 'wb')

running.stop_recording()

因此我将在两个函数中使用它- start stop -并且我不需要任何循环.

so I would use it in two functions - start and stop - and I wouldn't need any loop for this.

我只需要全局变量 running 即可访问这两个功能中的记录器.

I would need only global variable running to have access to recorder in both functions.

import tkinter as tk
import recorder

# --- functions ---

def start():
    global running

    if running is not None:
        print('already running')
    else:
        running = rec.open('nonblocking.wav', 'wb')
        running.start_recording()

def stop():
    global running

    if running is not None:
        running.stop_recording()
        running.close()
        running = None
    else:
        print('not running')

# --- main ---

rec = recorder.Recorder(channels=2)
running = None

root = tk.Tk()

button_rec = tk.Button(root, text='Start', command=start)
button_rec.pack()

button_stop = tk.Button(root, text='Stop', command=stop)
button_stop.pack()

root.mainloop() 

这篇关于Tkinter开始/停止按钮,用于以Python录制音频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆