📜  在Python中使用 Tkinter 的文本到语音 GUI 转换器

📅  最后修改于: 2022-05-13 01:55:22.168000             🧑  作者: Mango

在Python中使用 Tkinter 的文本到语音 GUI 转换器

先决条件: Tkinter 简介

Tkinter 是Python的标准 GUI 库。 Python与 tkinter 结合使用提供了一种快速简便的方式来创建 GUI 应用程序。通过这个库,我们可以为在Python中构建 GUI 应用程序做出令人信服的选择,特别是对于不需要现代光泽并且首要任务是构建一些东西的应用程序这是功能性和跨平台的快速。

要创建 tkinter 应用程序:

现在,让我们创建一个基于 GUI 的文本到语音转换器应用程序,它将文本转换为语音。

Python中有很多库,其中之一是 gTTS(Google Text-to-Speech),这是一个Python库和 CLI 工具,用于与 Google Translate 的 text-to-speech API 进行交互。

要安装gTTS ,只需转到您的终端并输入:

pip install gTTS

下面是实现:

Python3
# importing required module
from tkinter import *
from gtts import gTTS
 
# this module helps to
# play the converted audio 
import os
 
# create tkinter window
root = Tk()
 
# styling the frame which helps to 
# make our background stylish
frame1 = Frame(root,
               bg = "lightPink",
               height = "150")
 
# place the widget in gui window
frame1.pack(fill = X)
 
 
frame2 = Frame(root,
               bg = "lightgreen",
               height = "750")
frame2.pack(fill=X)
 
 
 
# styling the label which show the text 
# in our tkinter window
label = Label(frame1, text = "Text to Speech",
              font = "bold, 30",
              bg = "lightpink")
 
label.place(x = 180, y = 70)
 
 
 
# entry is used to enter the text 
entry = Entry(frame2, width = 45,
              bd = 4, font = 14)
 
entry.place(x = 130, y = 52)
entry.insert(0, "")
 
# define a function which can
# get text and convert into audio
def play():
 
    # Language in which you want to convert 
    language = "en"
 
 
 
   # Passing the text  and language, 
   # here we have  slow=False. Which tells 
   # the module that the converted audio should 
   # have a high speed 
 
    myobj = gTTS(text = entry.get(),
                lang = language,
                slow = False)
 
 
 
    # give the name as you want to 
    # save the audio
    myobj.save("convert.wav")
    os.system("convert.wav")
 
# create a button which holds
# our play function using command = play
btn = Button(frame2, text = "SUBMIT",
             width = "15", pady = 10,
             font = "bold, 15",
             command = play, bg='yellow')
 
btn.place(x = 250,
          y = 130)
 
# give a title 
root.title("text_to_speech_convertor")
 
 
 
# we can not change the size
# if you want you can change
root.geometry("650x550+350+200")
 
# start the gui
root.mainloop()


输出: