首页 > 解决方案 > 如何从 tkinter 中的按钮停止功能

问题描述

我正在尝试在 ffmpeg 的帮助下从 python 中的麦克风录制音频。

通过使用它,我可以录制语音,但是如何通过单击按钮来停止此功能。

为了阻止这种情况,我现在强制终止 IDLE。

import os
import subprocess
import tkinter as tk
from tkinter import *
root = Tk()
os.chdir('C://Users/ravir/desktop/')
def recording_voice():
  global p
  p=subprocess.Popen('ffmpeg -f dshow -i audio="Microphone (2- High Definition Audio Device)" sample.mp3' )

def stop_rec():  # ho to stop this ???
    p.terminate()

rec_btn = Button(text='Start Recording', width=20, command=recording_voice)
rec_btn.pack()

stop_btn = Button(text='Stop Recording', width=20, command=stop_rec)
stop_btn.pack()

root.mainloop()

标签: python-3.xtkinterffmpegsubprocess

解决方案


推荐阅读