运行截图
主要功能实现
1.文本转语音功能
async def conversion(text, output, role, rate, volume):
tts = edge_tts.Communicate(text=text, voice=role, rate=rate, volume=volume)
await tts.save(output)
2.轮播图实现
def carousel(self, img_index=0):
if img_index >= len(self.carousels):
img_index = 0
self.label2.config(image=self.carousels[img_index])
self.form.after(self.carousel_time * 1000, self.carousel, img_index + 1)
3.为了保证UI的流畅,定义Worker类利用线程池来执行耗时任务(可以用来执行同步任务和异步任务,任务完成后将返回值传递给回调函数when_done)
class Worker(object):
thread_pool = None
def __init__(self, task, when_done, *args, synchronous=True):
self._task = task
self._args = args
self.synchronous = synchronous
self.when_done = when_done
def _my_task(self):
try:
if self.synchronous:
res = self._task()
else:
res = asyncio.run(self._task(*self._args))
except Exception as e:
res = False, e
self.when_done(res)
def start(self):
if not Worker.thread_pool:
Worker.thread_pool = ThreadPoolExecutor(max_workers=1)
Worker.thread_pool.submit(self._my_task)
代码结构
最后欢迎大家在评论区讨论