使用Tkinter在while循环中录制语音 [英] Recording voice in a while loop using Tkinter
问题描述
我正在编写语音录制应用程序,我想在用户按下Tkinter时开始录制,并在用户释放Button时停止录制.
I'm coding a voice recording application and I want to start the recording when the user pushes a Button in Tkinter, and stop the recording when the user releases the Button.
import Tkinter
def record():
while True
Recordning runtines...
if <button is released>
stop audio steam...
break
main = Tkinter.Tk()
b = Tkinter.Button(main, text='rec', command=record)
b.place(x="10", y="10")
main.mainloop()
如何实现如果释放按钮"?我需要使用线程处理吗?
How do I achieve the "if button is released"? Do I need to use Threading?
推荐答案
如果您不想在录制时冻结GUI,建议您使用多线程.可以通过事件 <Button-1>
和<ButtonRelease-1>
.我已经将代码包装到一个类中,因此它还包含用于完成工作线程的标志.
If you don't want to freeze the GUI while recording, I recommend you to use multithreading. The click and release of the button can be accomplished with the events <Button-1>
and <ButtonRelease-1>
. I have wrapped the code into a class, so it also contains the flag for finishing the working thread.
import Tkinter as tk
import threading
class App():
def __init__(self, master):
self.isrecording = False
self.button = tk.Button(main, text='rec')
self.button.bind("<Button-1>", self.startrecording)
self.button.bind("<ButtonRelease-1>", self.stoprecording)
self.button.pack()
def startrecording(self, event):
self.isrecording = True
t = threading.Thread(target=self._record)
t.start()
def stoprecording(self, event):
self.isrecording = False
def _record(self):
while self.isrecording:
print "Recording"
main = tk.Tk()
app = App(main)
main.mainloop()
这篇关于使用Tkinter在while循环中录制语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!